Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
def comb(val):
    if(val > 0):
        return int(val*(val-1) / 2)
    else:
        return 0
    
def make_map(balls):
    balls_map = {}
    for ball in balls:
        if(ball in balls_map):
            balls_map[ball] += 1
        else:
            balls_map[ball] = 1
    return balls_map

def main():
    args = input().rstrip().split(' ')
    N = int(args[0])
    balls = [val for val in input().rstrip().split(' ')]
    balls_map = make_map(balls)
    
    for ball in balls:
        balls_map[ball] -= 1
        sum_ = 0
        for val in balls_map.values():
            sum_ += comb(val)
        print(sum_)
        balls_map[ball] += 1
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
s = 0
for a in range(1,N+1):
  t = A.count(a)
  if t>= 2:
    s += (t-1)*t/2
  
for k in range(1,N+1):
  i = A[k-1]
  I = A.count(i)
  if I < 2:
    print(int(s))
  else:
    print(int(s - (I-1)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
uA = list(set(A))
c = {u:A.count(u) for u in uA}
 
la = {}
lb = {}
 
for a in uA:
  la[a] = int(c[a]*(c[a]-1)/2)
  lb[a] = int((c[a]-1)*(c[a]-2)/2)
    
for a in A:
  b = lb[a] + sum([v for k,v in la.items() if k!=a])
  print(b)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
uA = list(set(A))
c = {u:A.count(u) for u in uA}
 
la = {}
lb = {}
 
for a in uA:
  la[a] = int(c[a]*(c[a]-1)/2)
  lb[a] = int((c[a]-1)*(c[a]-2)/2)
  
for a in A:
  ks = la.values()
  b = lb[a] + sum([v for k,v in la.items() if k!=a])
  print(b)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
def ta():
  l = A[0]
  A.pop(0)
  co = 0
  for s in range(1,N+1):
    t = A.count(s)
    if t >= 2:
      co += t*(t-1)/2
    else:
      co += 0
  A.append(l)
  return co
  
for k in range(1,N+1):
  print(int(ta()))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
c = 0
d = {}
for a in set(A):
  d[a] = A.count(a)
  c += d[a] * (d[a] - 1) / 2
for a in A:
  print(int(c - (d[a] - 1)))

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
lin = input().split()
# 要素の個数を求める
dic_s = {}
for c in lin:
  dic_s.setdefault(c, 0)
  dic_s[c] += 1
dic_tot = {}
for c in lin:
  if dic_tot.get(c):
    print(int(dic_tot[c]))
  else:
    tot = 0
    for key,value in dic_s.items():
      v = value
      if key == c:
        v -= 1
      if v > 1:
        tot += v * (v - 1) / 2
    dic_tot.setdefault(c,tot)
    print(int(tot))

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
lin = input().split()
# 要素の個数を求める
dic_s = {}
for c in lin:
  dic_s.setdefault(c, 0)
  dic_s[c] += 1
for c in lin:
  tot = 0
  for key,value in dic_s.items():
    v = value
    if key == c:
      v -= 1
    if v > 1:
      tot += v * (v - 1) / 2
  print(int(tot))

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
lin = input().split()
i = 0
while i < len(lin):
  sl = []
  sr = []
  if i > 0:
    sl = lin[0:i]
  if i < len(lin)-1:
    sr = lin[i+1:]
  # print("sl,sr=",sl,sr)
  sl.extend(sr)
  s = sl
  i+=1
  # 要素の個数を求める
  dic_s = {}
  for c in s:
    dic_s.setdefault(c, 0)
    dic_s[c] += 1
  tot = 0
  for key,value in dic_s.items():
    if value > 1:
      tot += value * (value - 1) / 2
  print(int(tot))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import mul
from functools import reduce
def combinations_count2(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

#
N=int(input())
A=list(map(int,input().split()))
#
hoge=Counter(A)
ans=0
for i in hoge.values():
    if i>=2:
        ans += combinations_count2(i, 2)
for i in range(N):
    print(ans-A.count(A[i])+1)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from collections import deque
n = int(input())
a = deque(list(map(int, input().split())))
for i in range(n):
    num = 0
    b = a.popleft()
    for j in range(n):
        c = a.count(j)
        num += int(c*(c-1)/2)
    print(num)
    a.append(b)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    num = 0
    b = a.pop(0)
    for j in range(n):
        c = a.count(j)
        num += int(c*(c-1)/2)
    print(num)
    a.append(b)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import mul
from functools import reduce
def combinations_count2(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

#
N=int(input())
A=list(map(int,input().split()))
#
hoge=Counter(A)
ans1=0
for i in hoge.values():
    if i==1:
        continue
    else:
        ans1+=combinations_count2(i,2)

for i in range(N):
    print(ans1-A.count(A[i])+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import mul
from functools import reduce

def combinations_count2(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom
#
N=int(input())
A=list(map(int,input().split()))
#
hoge=Counter(A)
ans1=0
for i in hoge.values():
    if i==1:
        continue
    else:
        ans1+=combinations_count2(i,2)

ans2=[]
for i in A:
    ans2.append(A.count(i)-1)

for k in range(N):
    print(ans1-ans2[k])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
def combinations_count1(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
#
N=int(input())
A=list(map(int,input().split()))
#
hoge=Counter(A)
ans1=0
for i in hoge.values():
    if i==1:
        continue
    else:
        ans1+=combinations_count1(i,2)

ans2=[]
for i in A:
    ans2.append(A.count(i)-1)

for k in range(N):
    print(ans1-ans2[k])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
count=[0]*(n+1)
for i in range (n):
    count[a[i]]+=1
for i in range(n):
    count[a[i]]-=1
    c=0
    for j in range(1,n+1):
        c=c+count[j]*(count[j]-1)//2
    print(c)
    count[a[i]] +=1

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
count=[0]*(n+1)
output=[0]*(n+1)
for i in range (n):
    count[a[i]]+=1
#print(count)
for i in range(n):
    count[a[i]]-=1
    #print(count)
    c=0
    for j in range(1,n+1):
        c=c+count[j]*(count[j]-1)//2
    output[i]=c
    count[a[i]] +=1

for i in range (n):
    print(output[i])

Submission status: Time Limit Exceeded

Solution code:
# from scipy.misc import comb
# from scipy.special import comb
import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def banned(al):
    d = {}
    for a in al:
        if a in d.keys():
            d[a] += 1
        else:
            d[a] = 1
    # print(d)
    for k, v in d.items():
        if v > 1:
            d[k] = [v, comb(v, 2)]
        else:
            d[k] = [v, 0]
    for a in al:
        ans = 0
        for k, v in d.items():
            if k == a:
                if v[0] > 2:
                    ans += comb(v[0]-1, 2)
            else:
                ans += v[1]
        print(int(ans))

n = int(input())
al = [int(i) for i in input().split()]

banned(al)

Submission status: Time Limit Exceeded

Solution code:
import collections

def comb2(n):
    return int(n * (n - 1) / 2)

def dictcomb(dic, dp):
    ans = 0
    for key in dic:
        ans += dp[dic[key]]
    return ans

def solve(a, dic, dp):
    for i in a:
        dic[i] -= 1
        print(dictcomb(dic, dp))
        dic[i] += 1

def main():
    dp = [comb2(n) for n in range(2 * (10 ** 5) + 1)]
    n = int(input())
    a = list(map(int,input().split()))
    d = collections.Counter(a)
    solve(a, d, dp)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

def comb2(n):
    return int(n * (n - 1) / 2)

def dictcomb(dic):
    ans = 0
    for key in dic:
        ans += comb2(dic[key])
    return ans

def solve(a, dic):
    for i in a:
        dic[i] -= 1
        print(dictcomb(dic))
        dic[i] += 1

def main():
    n = int(input())
    a = list(map(int,input().split()))
    d = collections.Counter(a)
    solve(a, d)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
C = list(map(int,input().split()))

def chose(A):
    s = 0
    t = 0
    for i in range(len(A)):
        t = A.count(i+1) 
        for q in range(t):
            s += q
    print(s)

for i in range(N):
    B = C[:]
    B.pop(i)
    chose(B)

Submission status: Time Limit Exceeded

Solution code:
import collections
def choose2(n):
    return n*(n-1)/2
sum1=0
sum2=0
N = int(input())
A = [int(N) for N in input().split()]
c = collections.Counter(A)
values, counts = zip(*c.most_common())
for i in range(len(counts)):
    sum1+=int(choose2(counts[i]))
for j in range(N):
    for k in range(len(counts)):
        if A[j] == values[k]:
            sum2+=int(sum1-choose2(counts[k])+choose2(counts[k]-1))
            print(sum2)
            sum2=0
            break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]

ans = 0
for i in range(n):
    j = l.count(i)
    ans += int(j * (j - 1) / 2)

for i in range(n):
    ans_i = 0
    j = l.count(l[i])
    print(ans - j + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]


ans = 0
for i in range(n):
    j = l.count(i)
    ans += int(j * (j - 1) / 2)

for i in range(n):
    ans_i = 0
    j = l.count(l[i])
    print(ans - j + 1)


for i in range(3, n + 1):
    l.count(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]


ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        if l[j] == l[i]:
            ans += 1

for i in range(n):
    ans_i = 0
    for j in range(n):
        if j != i:
            if l[i] == l[j]:
                ans_i += 1
    print(ans - ans_i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
for i in range(n):
    answer = 0
    for j in range(n - 1):
        ans = 0
        if j == i:
            continue
        for k in range(j + 1, n):
            if k != i:
                if l[k] == l[j]:
                    ans += 1
        answer += ans
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())

AD = dict()
for a in A:
    if a not in AD.keys():
        AD[a] = 1
    else:
        AD[a] += 1

SUM = 0
for key in AD.keys():
    SUM += (AD[key] * (AD[key] - 1)) / 2

for k in A:
    print SUM - (AD[k] - 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())

AD = dict(); AS = set(A)
SUM = 0
for ass in AS:
    AD[ass] = A.count(ass)
    SUM += (AD[ass] * (AD[ass] - 1)) / 2

for k in A:
    print SUM - (AD[k] - 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = list(map(int, raw_input().split()))

AD = dict(); AS = set(A)
SUM = long(0)
for ass in AS:
    AD[ass] = A.count(ass)
    SUM += long(long((AD[ass] * (AD[ass] - 1))) / 2)

for k in A:
    print SUM - (AD[k] - 1)

Submission status: Time Limit Exceeded

Solution code:
import collections
sum = 0
#Asum=[]
N = int(input())
A =  [int(N) for N in input().split()]
B = A.copy()
for i in range(N):
    B.pop(i)
    c = collections.Counter(B)
    values, counts = zip(*c.most_common())
    for j in range(len(counts)):
        if counts[j]>1:
            sum+=int(counts[j]*(counts[j]-1)/2)
        else:
            break
    B=A.copy()
    #Asum.append(sum)
    print(sum)
    sum=0
#for k in range(N):
    #print(Asum[k])

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = list(map(int, raw_input().split()))

AD = dict(); AS = set(A)
SUM = 0
for ass in AS:
    AD[ass] = A.count(ass)
    SUM += (AD[ass] * (AD[ass] - 1)) / 2

for k in A:
    print SUM - (AD[k] - 1)

Submission status: Time Limit Exceeded

Solution code:
import collections
sum = 0
Asum=[]
N = int(input())
A =  [int(N) for N in input().split()]
B = A.copy()
for i in range(N):
    B.pop(i)
    c = collections.Counter(B)
    values, counts = zip(*c.most_common())
    for j in range(len(counts)):
        if counts[j]>1:
            sum+=int(counts[j]*(counts[j]-1)/2)
        else:
            break
    B=A.copy()
    Asum.append(int(sum))
    sum=0
for k in range(N):
    print(Asum[k])

Submission status: Time Limit Exceeded

Solution code:
import collections
sum = 0
Asum=[]
N = int(input())
A =  [int(N) for N in input().split()]
B = A.copy()
for i in range(N):
    B.pop(i)
    c = collections.Counter(B)
    values, counts = zip(*c.most_common())
    for j in range(len(counts)):
        sum+=int(counts[j]*(counts[j]-1)/2)
    B=A.copy()
    Asum.append(int(sum))
    sum=0
for k in range(N):
    print(Asum[k])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = {}
def_count = 0
for i in a_uni:
    a_co = a.count(i)
    a_ol[i] = a_co
    def_count += a_co * (a_co - 1)

def_count //= 2

for k in a:
    print(def_count - a_ol[k] + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = {}
def_count = 0
for i in a_uni:
    a_co = a.count(i)
    a_ol[i] = a_co
    def_count += a_co * (a_co - 1)

def_count //= 2

for k in range(n):
    print(def_count - a_ol[a[k]] + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = {}
def_count = 0
for i in a_uni:
    a_co = a.count(i)
    a_ol[i] = a_co
    def_count += a_co * (a_co - 1) // 2

for k in range(n):
    print(def_count - a_ol[a[k]] + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = {}
def_count = 0
for i in a_uni:
    a_co = a.count(i)
    if a_co > 1:
        a_ol[i] = a_co
        def_count += a_co * (a_co - 1) // 2

for k in range(n):
    if a[k] in a_ol:
        print(def_count - a_ol[a[k]] + 1)
    else:
        print(def_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = {}

for i in a_uni:
    a_co = a.count(i)
    if a_co > 1:
        a_ol[i] = a_co


def_count = sum([i * (i - 1) // 2 for i in a_ol.values()])
for k in range(n):
    if a[k] in a_ol:
        print(def_count - (a_ol[a[k]] - 1))
    else:
        print(def_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = []
a_counts = []
for i in a_uni:
    a_co = a.count(i)
    if a_co > 1:
        a_ol.append(i)
        a_counts.append(a_co)


def_count = sum([i * (i - 1) // 2 for i in a_counts])
for k in range(n):
    count = 0
    if a[k] in a_ol:
        count = def_count - (a_counts[a_ol.index(a[k])] - 1)
    else:
        count = def_count
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

a_ol = []
a_counts = []
for i in a_uni:
    a_co = a.count(i)
    if a_co > 1:
        a_ol.append(i)
        a_counts.append(a_co)

def_count = sum([i * (i - 1) // 2 for i in a_counts])
for k in range(n):
    count = 0
    if a[k] in a_ol:
        a_counts[a_ol.index(a[k])] -= 1
        count = sum([i * (i - 1) // 2 for i in a_counts])
        a_counts[a_ol.index(a[k])] += 1
    else:
        count = def_count
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_uni = list(set(a))

for k in range(n):
    b = a.copy()
    del b[k]
    count = 0
    for i in a_uni:
        ol = b.count(i)
        count += ol * (ol - 1) // 2
    print(count)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import collections

N = int(input())
A = list(map(int, input().split()))

c = collections.Counter(A)
values, counts = zip(*c.most_common())

ans = 0
for i in range(len(values)):
    tmp = counts[i]
    ans += (tmp * (tmp-1))//2

for i in range(N):
    num = A[i]
    index = values.index(num)
    print(ans - counts[index] + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

def cmb(n):
    return int(n*(n-1)/2)

goukei = 0
count = 0

for i in range(n):
    if a[i] in a[:i]:
        continue
    else:
        count = a.count(a[i])
        goukei += cmb(count)

for i in range(n):
    print(goukei - (a.count(a[i]) - 1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

# def cmb(n):
#     return int(n*(n-1)/2)

b = []
c = []
goukei = 0

for i in range(n):
    l = a.count(a[i])
    b.append(int(l*(l-1)/2))
    c.append(int((l-1)*(l-2)/2))
    if a[i] in a[:i]:
        continue
    else:
        goukei += int(l*(l-1)/2)

# print(b)
# print(c)
# print(goukei)

for i in range(n):
    print(goukei + c[i] - b[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

def cmb(n):
    return int(n*(n-1)/2)

b = []
c = []
goukei = 0

for i in range(n):
    l = a.count(a[i])
    b.append(cmb(l))
    c.append(cmb(l-1))
    if a[i] in a[:i]:
        continue
    else:
        goukei += cmb(l)

# print(b)
# print(c)
# print(goukei)

for i in range(n):
    print(goukei + c[i] - b[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))

def cmb(n):
    return int(n*(n-1)/2)

b = []
c = []
d = []

for i in range(n):
    if a[i] in a[:i]:
        continue
    else:
        l = a.count(a[i])
        b.append(cmb(l))
        c.append(cmb(l-1))
        d.append(a[i])

goukei = sum(b)

for i in range(n):
    e = d.index(a[i])
    print(c[e] + goukei - b[e])

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

n = int(input())
li = input().split()


def combinations_count(n, r):
    try:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    except ValueError:
        return 0


    
for i in range(0, n):
    s = 0
    a = li[i]
    del li[i]
    c = collections.Counter(li)
    for k in c.values():
        s += combinations_count(k,2)
    print(s)
    li.insert(i,str(a))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
import collections
import math
def combinations_count2(n):
    if(n<=1):
        answer=0
    else:
        answer=n*(n-1)/2
    return answer

table=collections.Counter(a)
keytable=table.keys()
c=[]

for key in keytable:
    c.append(table[key])
#print(c)
ca=[]
for j in range(len(c)):
    ca.append(combinations_count2(c[j]))
#print(ca)
sums=sum(ca)
#print(sum)
#print(keytable)
#print(table)
for i in range(len(a)):
    keynum=list(keytable).index(a[i])
    answer=sums-c[keynum]+1
    print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

n = int(input())
li = input().split()


def combinations_count(n, r):
    try:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    except ValueError:
        return 0


    
for i in range(0, n):
    s = 0
    a = li[i]
    del li[i]
    c = collections.Counter(li)
    for k in c.values():
        s += combinations_count(k,2)
    print(s)
    li.insert(i,str(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(item)for item in input().split()]
c=Counter(A)
full = {i: c[i] * (c[i] - 1) / 2 for i in c.keys()}
#full = [full[item-1] for item in A]
#print(full)
#ichiheri = [c[i]-1 * (c[i]-1) / 2 for i in range(1,1+len(c))]
ichiheri = {i:(c[i]-2) * (c[i]-1) / 2 for i in c.keys()}
#print(ichiheri)

for a in A:
    sumA = sum(full.values())
    sumA-=full[a]
    sumA += ichiheri[a]
    print(int(sumA))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
n=int(input())
a=list(map(int,input().split()))
c=list(range(1,n+1))
num=list(map(lambda t:reduce(lambda a,b:a+1 if b==t else a,a,0),c))
preans=reduce(lambda x,y:x+y*(y-1)/2,num,0)
for i in range(n):
    print(int(preans-num[a[i]-1]+1))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
n=int(input())
a=list(map(int,input().split()))
c=list(range(1,n+1))
num=list(map(lambda t:reduce(lambda a,b:a+1 if b==t else a,a,0),c))
"""
test=list(map(lambda t:map(lambda x,y:x if (y!=t or x==0) else x-1,num,c),c))
for i in range(5):
    print(list(test[i]))
"""
preans=reduce(lambda x,y:x+y*(y-1)/2,num,0)

for i in range(n):
    print(int(preans-num[a[i]-1]+1))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a_lst = [int(elem) for elem in input().split()]
c = collections.Counter(a_lst)
res1 = 0
for k in c.keys():
  if c[k] > 1:
    res1 += c[k] * (c[k] - 1) // 2

for i in range(n):
  res = res1
  lst = a_lst[:i]+a_lst[i+1:]
  res -= lst.count(a_lst[i])
  print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

n = int(input())
a_lst = [int(elem) for elem in input().split()]
c = collections.Counter(a_lst)
res1 = 0
for k in c.keys():
  if c[k] > 1:
    res1 += math.factorial(c[k]) // (math.factorial(c[k] - 2) * math.factorial(2))

for i in range(n):
  res = res1
  lst = a_lst[:i]+a_lst[i+1:]
  res -= lst.count(a_lst[i])
  print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

n = int(input())
a_lst = [int(elem) for elem in input().split()]

for i in range(n):
  res = 0
  lst = a_lst[:i]+a_lst[i+1:]
  c = collections.Counter(lst)
  for k in c.keys():
    if c[k] > 1:
      res += math.factorial(c[k]) // (math.factorial(c[k] - 2) * math.factorial(2))
  print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=tuple(map(int,input().split()))
b=[0]*n
x=0
for i in set(a):
  b[i-1]=a.count(i)
  x+=(b[i-1]*(b[i-1]-1))//2
for i in a:
  y=b[i-1]-1
  print(x-y if x>=y else 0)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
a = list(map(int,input().split()))
b = a.copy()
b = set(b)
b = list(b)
c = []
ans = []

for i in range(len(b)):
    c.append(a.count(b[i]))
d = []
for i in range(len(b)):
    num = 0
    for j in range(len(b)):
        if i == j:
            num += int(((c[j]-1)*(c[j]-2))/2)
        else:
            num += int(((c[j])*(c[j]-1))/2)
    d.append(num)

for i in range(n):
    for j in range(len(b)):
        if a[i] == b[j]:
            print(d[j])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

def main():
    ball_count = int(input())
    balls = list(map(int, input().split()))
    variants = {}
    nums = {}
    for i in set(balls):
        nums.update({i: balls.count(i)})

    for k in range(ball_count):
        count = 0
        total = 0

        nums[balls[k]] -= 1

        for v in nums.values():
            if v not in variants:
                if v >= 2:
                    count = (v*(v-1)) // factorial(2)
                variants.update({v: count})
                total += count
                count = 0
            else:
                total += variants[v]
        nums[balls[k]] += 1
        print(total)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    ball_count = int(input())
    balls = list(map(int, input().split()))
    variants = {}
    nums = {}
    for i in set(balls):
        nums.update({i: balls.count(i)})

    for k in range(ball_count):
        count = 0
        total = 0

        nums[balls[k]] -= 1

        for v in nums.values():
            if v not in variants:
                if v >= 2:
                    count += 1
                    for i in range(3, v + 1):
                        count = count - 1 + i
                variants.update({v: count})
                total += count
                count = 0
            else:
                total += variants[v]
        nums[balls[k]] += 1
        print(total)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    ball_count = int(input())
    balls = list(map(int, input().split()))
    variants = {}

    for k in range(ball_count):
        count = 0
        nums = {}
        total = 0
        for i in range(len(balls)):
            if i == k:
                continue

            if balls[i] not in nums:
                nums[balls[i]] = 0

            nums[balls[i]] += 1

        for v in nums.values():
            if v not in variants:
                if v >= 2:
                    count += 1
                    for i in range(3, v + 1):
                        count = count - 1 + i
                variants.update({v: count})
                total += count
                count = 0
            else:
                total += variants[v]


        print(total)


if __name__ == '__main__':
    main()


#{1: 2, 2: 2}
# 2
# {1: 2, 2: 2}
# 2
# {1: 3, 2: 1}
# 3
# {1: 2, 2: 2}
# 2
# {1: 3, 2: 1}
# 3

Submission status: Time Limit Exceeded

Solution code:
def main():
    ball_count = int(input())
    balls = list(map(int, input().split()))


    for k in range(ball_count):
        count = 0
        nums = {}
        for i in range(len(balls)):
            if i == k:
                continue

            if balls[i] not in nums:
                nums[balls[i]] = 0

            nums[balls[i]] += 1

        for x, v in nums.items():

            if v >= 2:
                count += 1
                for i in range(3, v+1):
                    count = count-1+i

        print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
diff_dict = {}
total_sum = 0
for i in a:
    if i not in diff_dict:
        i_count = a.count(i)
        temp = (i_count)*(i_count-1)//2
        diff_dict[i] = temp-(i_count-1)*(i_count-2)//2
        total_sum += temp
for i in a:
    print(total_sum - diff_dict[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
count_dict = {}
total_sum = 0
for i in a:
    if i not in count_dict:
        count_dict[i] = a.count(i)
        total_sum += (count_dict[i])*(count_dict[i]-1)//2
for i in a:
    print(total_sum - (count_dict[i]*(count_dict[i]-1)//2) + (count_dict[i]-1)*(count_dict[i]-2)//2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
count_dict = {}
total_sum = 0
for i in a:
    if i not in count_dict:
        count_dict[i] = a.count(i)
        total_sum += (a.count(i))*(a.count(i)-1)//2
for i in a:
    print(total_sum - (a.count(i)*(a.count(i)-1)//2) + (a.count(i)-1)*(a.count(i)-2)//2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_li = list(map(int,input().split()))

from collections import Counter
c = Counter(a_li)
for x in a_li:
    ans = 0
    tmp = c.copy()
    tmp[x] -= 1
    for y in tmp.values():
        ans += y*(y-1)//2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_li = list(map(int,input().split()))

from collections import Counter
c = Counter(a_li)
for x in a_li:
    ans = 0
    tmp = c.copy()
    tmp[x] -= 1
    for y in tmp.values():
        ans += sum([x for x in range(y)])
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_li = list(map(int,input().split()))

from collections import Counter
import math
def combinations_count(n, r):
    if n-r<0:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

c = Counter(a_li)
for x in a_li:
    ans = 0
    tmp = c.copy()
    tmp[x] -= 1
    for y in tmp.values():
        ans += combinations_count(y,2)
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = input()
a = list(map(int, input().split()))
number_dict = {}
for i in a:
    if i not in number_dict:
        number_dict[i] = int(a.count(i) * (a.count(i)-1) / 2)
total_comb = sum(number_dict.values())
for i in a:
    diff = number_dict[i] - int((a.count(i)-1) * (a.count(i)-2)/2)
    print(total_comb - diff)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
def kaijyou(k):
  N=1
  for i in range(k,0,-1):
    N*=i
  return N

def combinations(n,r):
  return int(kaijyou(n)/(kaijyou(r)*kaijyou(n-r)))

N=int(input())
A=list(map(int,input().split()))
B=sorted(set(A))
dp=[]

for i in B:
  C=copy.copy(A)
  C.remove(i)
  c=collections.Counter(C)
  e=c.values()
  f=0
  for k in e:
    f+=combinations(k,2)
  dp.append(f)
for i in A:
  print(dp[B.index(i)])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
number = [0] * N
total = 0
for i in range(N):
  tmp = A.count(i + 1)
  total += tmp * (tmp -1) // 2
  number[i] = tmp
for i in range(N):
  number[i] = total - number[i] + 1
for j in A:
  print(number[j-1])

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
def kaijyou(k):
  N=1
  for i in range(k,0,-1):
    N*=i
  return N

def combinations(n,r):
  return int(kaijyou(n)/(kaijyou(r)*kaijyou(n-r)))

N=int(input())
A=list(map(int,input().split()))
B=sorted(set(A))
d=[]
dp=[]

for i in B:
  C=copy.copy(A)
  C.remove(i)
  c=collections.Counter(C)
  d.append(c)

for i in d:
  e=i.values()
  f=0
  for k in e:
    f+=combinations(k,2)
  dp.append(f)
for i in A:
  print(dp[B.index(i)])