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:
n=int(input())
A=list(map(int,input().split()))

D={}
for i in A:
      if i not in D:
            D[i]=1
      else:
            D[i]+=1


for i in range(n):
      D[A[i]]-=1
      ans=0
      for j in D.values():
            ans+=j*(j-1)//2
      print(ans)
      D[A[i]]+=1

Submission status: Time Limit Exceeded

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

a_set = sorted(set(a))
cnt_dict = {}


def f(n):
    return (n * (n - 1) // 2)


sum = 0
for i in a_set:
    cnt_dict[i] = a.count(i)
    sum += f(cnt_dict[i])


for i in a:
    print(sum - (cnt_dict[i] - 1))

Submission status: Time Limit Exceeded

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

a_set = sorted(set(a))
a_dict = {}
cnt_dict = {}


def f(n):
    sum = 0
    for i in range(n):
        sum += i
    return sum


sum = 0
for i in a_set:
    cnt_dict[i] = a.count(i)
    sum += f(cnt_dict[i])


for i in a_set:
    a_dict[i] = sum - (cnt_dict[i] - 1)


for i in a:
    print(a_dict[i])

Submission status: Time Limit Exceeded

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

p = {}
t = sorted(list(set(a)))
s = [0]*len(t)
for i in range(len(t)):
  l = a.count(t[i])
  p[t[i]] = l
#print(p)
for i in a:
  s = 0
  x = p[i]
  p[i] -= 1
  for j in t:
    for k in range(p[j]):
      s += k
  print(s)
  p[i] = x

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
a = np.array(list(map(int,input().split())))
for i in np.arange(N):
    b = np.delete(a, i)
    count = 0
    for j in np.arange(N-1):
        k = j+1
        while(k<N-1):
            if b[j] == b[k]:
                count = count + 1
            k = k + 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
def nPr(n, r):
  ans = 1
  for i in range(n,n-r,-1):
    ans *= i
  return ans

def nCr(n, r):
  tmp = 1
  for i in range(r,1,-1):
    tmp *= i
  return int(nPr(n, r) / tmp)

n = int(input())
a = list(map(int,input().split()))
count = len(a)
d = {}
x = {}

for i in a:
  if i not in d:
    d[i] = 1
  else:
    d[i] += 1

for i in d:
  x[i] = nCr(d[i], 2)

for i in a:
  ans = 0
  for j in d:
    if i == j:
      if d[j] > 2:
        ans += nCr(d[j]-1, 2)
    else:
      if d[j] > 1:
        ans += x[j]
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
def nPr(n, r):
  ans = 1
  for i in range(n,n-r,-1):
    ans *= i
  return ans

def nCr(n, r):
  tmp = 1
  for i in range(r,1,-1):
    tmp *= i
  return int(nPr(n, r) / tmp)

n = int(input())
a = list(map(int,input().split()))
count = len(a)
d = {}

for i in a:
  if i not in d:
    d[i] = 1
  else:
    d[i] += 1
#print(d)

for i in a:
  ans = 0
  for j in d:
    if i == j:
      if d[j] > 2:
        ans += nCr(d[j]-1, 2)
    else:
      if d[j] > 1:
        ans += nCr(d[j], 2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
b=set(a)
c=list(b)
d=len(b)
ans=0
for i in range(d):
    e=a.count(c[i])
    ans+=int(e*(e-1)/2)
for i in range(n):
    h=a.count(a[i])
    print(ans-h+1)

Submission status: Time Limit Exceeded

Solution code:
import math
from operator import mul
from functools import reduce

n=int(input())
a=list(map(int, input().split()))
b=set(a)
c=list(b)
d=len(b)
ans=0
def f(n,r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
for i in range(d):
    e=a.count(c[i])
    ans+=int(e*(e-1)/2)
for i in range(n):
    h=a.count(a[i])
    print(ans-h+1)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

c=0
a_uniq=list(set(a))

for i in a_uniq:
  c+=sum(range(1, a.count(i)))

for i in range(0,n):
  print('{:.0f}'.format(c-a.count(a[i])+1))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

c=0
a_uniq=list(set(a))

for i in a_uniq:
  c+=a.count(i)*(a.count(i)-1)/2

for i in range(0,n):
  print('{:.0f}'.format(c-a.count(a[i])+1))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

for i in range(0,n):
  c=0
  a_c=copy.copy(a)
  a_c.pop(i)
  a_uniq=list(set(a_c))
  for i in a_uniq:
    c+=a_c.count(i)*(a_c.count(i)-1)/2
  print('{:.0f}'.format(c))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

for i in range(0,n):
  a_c=copy.copy(a)
  a_c.pop(i)
  a_uniq=list(set(a_c))
  c=0
  for i in a_uniq:
    x=a_c.count(i)
    c+=x*(x-1)/2
  print('{:.0f}'.format(c))

Submission status: Time Limit Exceeded

Solution code:
import math
num=int(input())
target=list(map(int, input().split()))

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

for i in range(num):
    A=target.copy()
    A.pop(i)
    out=0
    for j in set(A):
        if A.count(j)>1:
            base = comb(A.count(j),2)
            out=out+base
    print(out)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
dic={}

def comb2(x):
    return (x)*(x-1)//2

for i in range(n):
    if(l[i] not in dic.keys()):
        dic[l[i]]=1
    else:
        dic[l[i]]+=1

for i in range(len(l)):
    sum=0
    for j in dic:
        if(l[i]==j):
            sum+=comb2(dic[j]-1)
        else:
            sum+=comb2(dic[j])
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = np.zeros(N+1, dtype = 'int32')
for a in A:
    count[a] += 1
    idx.add(a)

sum = 0
for i in idx:
    sum += count[i] * (count[i] - 1) // 2

for a in A:
    print(sum - count[a] + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = np.zeros(N+1, dtype = 'int32')
for a in A:
    count[a] += 1

sum = 0
for c in count:
    sum += c * (c - 1) // 2

for a in A:
    print(sum - count[a] + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = np.zeros(N+1, dtype = 'int32')
for a in A:
    count[a] += 1

sum = 0
for i in range(1, N+1):
    sum += count[i] * (count[i] - 1) // 2

for a in A:
    print(sum - count[a] + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = np.array([0]*(N+1), dtype = 'int32')
for a in A:
    count[a] += 1
    
sum = 0
for i in range(1, N+1):
    sum += count[i] * (count[i] - 1) // 2

for a in A:
    print(sum - count[a] + 1)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

a = int(input())

b = list(map(int, input().split()))

lis = [0]*(a+1)

for kk in range(a):
    lis[b[kk]] += 1
    
#print(lis)

for ll in range(a):
    count = 0
    new_list = copy.copy(lis)
    new_list[b[ll]] -= 1
    #print(new_list)
    for pp in range(a):
        if new_list[pp] > 0:
            count += (new_list[pp]*(new_list[pp]-1))/2
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(str, input().split()))
#print(A)
import collections as cl

def count_f(B:list):
	c = cl.Counter(B)
	c_sum = 0
	#print(c.keys())
	for i in c.keys():
		if c[i]>1:
			c_sum += c[i]*(c[i]-1)/2
	return print(int(c_sum))
	
B = []
for i in range(1,len(A)+1):
	B = A[:i-1]+A[i:]
	#print(B)
	#print(list_B)
	count_f(B)
	B = []

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.buffer.readline().rstrip())
    A = list(map(int, sys.stdin.buffer.readline().split()))
    P = set(A)

    obj = {}
    for p in P:
        obj[p] = A.count(p)

    p = 0
    for v in obj.values():
        if v > 1:
            p += v * (v - 1) // 2

    for a in A:
        print(p - (obj[a] - 1))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.buffer.readline().rstrip())
    A = list(map(int, sys.stdin.buffer.readline().split()))
    P = set(A)

    obj = {}
    for p in P:
        obj[p] = A.count(p)

    p = 0
    for v in obj.values():
        p += v * (v - 1) // 2

    for a in A:
        print(p - (obj[a] - 1))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
for k in range(N):
    count = 0
    for i in range(N):
        if (i == k):
            continue
        for j in range(i+1,N):
            if (j == k):
                continue
            if(a[i] == a[j]):
                count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
alist = list(map(int,input().split()))
aset = set(alist)
blist = list(aset)
from math import factorial
def cmb(n,r):
  a = factorial(n) // factorial(r) // factorial(n - r)
  return a
ans1 = sum([cmb(alist.count(i),2) for i in blist if alist.count(i) >= 2])
for i in range(n):
  if alist.count(alist[i]) >= 2:
    print(ans1-alist.count(alist[i])+1)
  else:
    print(ans1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
ball = list(map(int,input().split()))
for i in range(N):
  x = 0
  for j in range(len(ball)):
    for k in range(len(ball)):
      if j!=i and i!= k and k!= j and ball[j] == ball[k]:
        x += 1
  print(x//2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
alist = list(map(int,input().split()))
aset = set(alist)
blist = list(aset)
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
ans1 = sum([cmb(alist.count(i),2) for i in blist if alist.count(i) >= 2])
for i in range(n):
  if alist.count(alist[i]) >= 2:
    print(ans1-alist.count(alist[i])+1)
  else:
    print(ans1)

Submission status: Time Limit Exceeded

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

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1
for k in A:
    res = 0
    for key in nums.keys():
        cnt = nums.get(key) - 1 if key == k else nums.get(key)
        res += cnt*(cnt-1)//2
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
 
num=int(input())
target=list(map(int, input().split()))
def comb(N, r):
    return math.factorial(N) // (math.factorial(N - r) * math.factorial(r))
 
for i in range(num):
    A=target.copy()
    A.pop(i)
    out=0
    for j in set(A):
        try :
            base = comb(A.count(j),2)
        except:
            base=0
        out=out+base
    print(out)

Submission status: Time Limit Exceeded

Solution code:
import math

num=int(input())
target=list(map(int, input().split()))
def comb(N, r):
    return math.factorial(N) // (math.factorial(N - r) * math.factorial(r))

for i in range(num):
    A=target.copy()
    A.pop(i)
    out=0
    for j in set(A):
        try :
            base = comb(A.count(j),2)
        except:
            base=0
        out=out+base
    print(out)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

n = int(input())

an = list(map(int, input().split()))

an_np = np.array(an)

an_uni = np.unique(an)

an_cnt = np.empty(len(an_uni))

line_np = np.zeros(n)

for i, uni in enumerate(an_uni):
    an_cnt[i] = len(np.where(an_np == uni)[0])
an_cnt[an_cnt == 1] = 0
for i, num in enumerate(an_np):
    idx = np.where(an_uni == num)
    cnt = an_cnt[idx]
    l_cnt = sum(an_cnt * (an_cnt - 1) / 2)
    if l_cnt > 0:
        l_cnt -= (cnt - 1)
    line_np[i] = l_cnt
line_str = str(line_np)
print(line_str.strip("[]").replace(". ", "\n").strip("."))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import itertools
N = int(input())
A = list(map(int, input().split()))

def choose2(n):
    return len(list(itertools.combinations(range(n), 2)))

counts = Counter(A)
total = 0
for n, count in counts.items(): 
    #初めに、同じ数同士から2つ選んだ時の組み合わせの総和を求める
    combi = choose2(count)
    total += combi 

for i in A:
    #上で求めた組み合わせの総和から、消える数字について1つ減らしたときの組み合わせを減らす
    temp = total - choose2(counts[i])
    temp += choose2(counts[i] - 1) 
    print(temp)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
A = input().split()

A_set = set(A)

#ボールに書かれている 数字の内訳を辞書に登録
A_dic = collections.Counter(A)

#すべてのボールから同じボールを取り出す方法数
all_count = 0

for a in A_dic.keys():
    count_a = A.count(a)
    all_count += (count_a * (count_a - 1) // 2)

#n個あるボールから 1個取り除いた場合の方法数
#すべてのボールから取り出す方法数 - (n-1)通り

for i in range(N):
    print(all_count - A_dic.get(A[i])+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import itertools
N = int(input())
A = list(map(int, input().split()))

def choose2(n):
    return len(list(itertools.combinations(range(n), 2)))

counts = Counter(A)
total = 0
combi_count = [-1] * (N + 1)
for n, count in counts.items(): 
    #初めに、同じ数同士から2つ選んだ時の組み合わせの総和を求める
    combi = choose2(count)
    total += combi 
    combi_count[n] = combi

for i in A:
    #上で求めた組み合わせの総和から、消える数字について1つ減らしたときの組み合わせを減らす
    temp = total - combi_count[i]
    temp += choose2(counts[i] - 1) 
    print(temp)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
A = input().split()

A_set = set(A)

#ボールに書かれている 数字の内訳を辞書に登録
A_dic = collections.Counter(A)

#すべてのボールから同じボールを取り出す方法数
all_count = 0

for a in A_set:
    count_a = A.count(a)
    all_count += (count_a * (count_a - 1) // 2)

#n個あるボールから 1個取り除いた場合の方法数
#すべてのボールから取り出す方法数 - (n-1)通り

for i in range(N):
    print(all_count - A_dic.get(A[i])+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
alist = list(map(int,input().split()))
pcount = len(set(alist))
blist = list(set(alist))
ans = 0
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

for i in range(n):
  for j in blist:
    if alist[i] == j:
      if alist.count(j)-1>=2:
        ans += cmb(alist.count(j)-1,2)

    else:
      if alist.count(j)>=2:
        ans += cmb(alist.count(j),2)
  print(ans)
  ans = 0

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import itemgetter
import math

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

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

#N個のボールから、書かれている整数が等しいような異なる 2 つのボールを選び出す方法の数
for i in range(N):
    B = sorted(B,key=itemgetter(1),reverse = True)
    T = 0
    for j in range(len(B)):
        if B[j][1] > 1:
            T += combinations_count(B[j][1],2)
        else :
            break
#print(B)
#print(T)
for i in range(N):
    ans = 0
    for j in range(len(B)):
            if A[i] == B[j][0]:
                if B[j][1]-1 > 1:
                    ans += combinations_count(B[j][1]-1,2)
            else:
                if B[j][1] > 1:
                    ans += combinations_count(B[j][1],2)
    print(ans)

Submission status: Time Limit Exceeded

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

b = sorted(a)
b.append(0)
#print(b)

for i in range(N):
    flag = 0
    num = 1
    temp = 0
    sumnum = 0
    for j in range(N): 
        if a[i] == b[j]:
            flag=1
        if not b[j] == b[j+1]:
            if flag==1:
                num -= 1
            #print('num',num)
            sumnum += (num * (num-1)) / 2
            num=0
            flag=0
        num +=1
    print(int(sumnum))

Submission status: Time Limit Exceeded

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


ans_list = []


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


for k in range(N):
    a = A[::]
    a[k:k+1] = []
    a_set = list(set(a))
    _list = [a.count(s) for s in a_set]
    ans_list += [sum(list(map(lambda n: conbi(n), _list)))]

for ans in ans_list:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

n = int(input())

an = list(map(int, input().split()))

an_np = np.array(an)

an_uni = np.unique(an)

an_cnt = np.empty(len(an_uni))
for i, uni in enumerate(an_uni):
    an_cnt[i] = len(np.where(an_np == uni)[0])
an_cnt[an_cnt == 1] = 0
for num in an_np:
    idx = np.where(an_uni == num)
    cnt = an_cnt[idx]
    l_cnt = sum(an_cnt * (an_cnt - 1) / 2)
    if l_cnt > 0:
        l_cnt -= (cnt - 1)
    print(int(l_cnt))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import mul
from functools import reduce
def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
N = int(input())
A = input().split()
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            if j >= 2:
                count = count + cmb(j, 2)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque
from collections import Counter
from scipy.misc import comb
from copy import deepcopy

n = int(input())
a = list(map(int, input().split()))
cnt_n = 0
a_n = deepcopy(a)
c = Counter(a_n)
for key in c:
    cnt_n += comb(c[key], 2, exact=True)

for k in range(len(a)):
    a_n = deepcopy(a)
    a_k = deepcopy(a[k])
    a_n.pop(k)
    cnt_k = a_n.count(a_k)
    print(cnt_n - cnt_k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# D
N = int(input())
A = list(map(int, input().split()))

for i in range(N):
	pair=0
	for j in range(N):
		for k in range(j+1, N):
			if(i == j) or (i == k):
				continue
			if A[j] == A[k]:
				pair += 1
	print(pair)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import itemgetter
import math

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

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

B =[]
for i in range(N):
    B = A[0:i] + A[i+1:N]
    #print(B)
    B=Counter(B)
    C=list(B.items())
    C = sorted(C,key=itemgetter(1),reverse = True)
    #print(C)
    ans = 0
    for j in range(len(C)):
        if C[j][1] > 1:
            ans += combinations_count(C[j][1],2)
        else :
            break
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()

A_set = set(A)

#ボールに書かれている 数字の内訳を辞書に登録
A_dic = {}
#すべてのボールから同じボールを取り出す方法数
all_count = 0

for a in A_set:
    count_a = A.count(a)
    all_count += (count_a * (count_a - 1) // 2)
#    A_dic.update({a:A.count(a)})

#n個あるボールから 1個取り除いた場合の方法数
#すべてのボールから取り出す方法数 - (n-1)通り

for i in range(N):
    C =  A.count(A[i])
#    print(all_count - A_dic.get(A[i])+1)
    print(all_count - C + 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = input().split()
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        C[A[i]] -= 1
        d = [comb(j, 2, exact=True) for j in C.values()]
        count = sum(d)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N=int(input())
A=[int(x) for x in input().split()]
Com=0

A_C=Counter(A)

for name,x in A_C.items():
    Com+=int(x*(x-1)/2)
    
for i in range(N):
    y=A.count(A[i])
    print(Com-y+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(set(list_A))
an = 0
for i in range(len(list_B)):
    c = list_A.count(list_B[i])
    an += (c*(c-1))//2


for i in range(n):
    c = list_A.count(list_A[i])
    if c != 2:
        ans = an - c + 1
    else:
        ans = an - 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()

A_set = set(A)

#ボールに書かれている 数字の内訳を辞書に登録
A_dic = {}
#すべてのボールから同じボールを取り出す方法数
all_count = 0

for a in A_set:
    count_a = A.count(a)
    all_count += (count_a * (count_a - 1) // 2)
    A_dic.update({a:A.count(a)})

#n個あるボールから 1個取り除いた場合の方法数
#すべてのボールから取り出す方法数 - (n-1)通り

for i in range(N):
    print(all_count - A_dic.get(A[i])+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(set(list_A))
an = 0
for i in range(len(list_B)):
    c = list_A.count(list_B[i])
    an += (c*(c-1))//2


for i in range(n):
    c = list_A.count(list_A[i])
    if c != 2:
        ans = an - (c*(c-1))//2 + ((c-1)*(c-2))//2
    else:
        ans = an - 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = input().split()
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            if j >= 2:
                count = count + comb(j, 2, exact=True)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = input().split()
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            if j >= 2:
                count += comb(j, 2, exact=True)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            if j >= 2:
                count += comb(j, 2, exact=True)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N=int(input())
A=[int(x) for x in input().split()]
A_s=list(dict.fromkeys(A))
Com=0

A_C=Counter(A)

for name,x in A_C.items():
    Com+=int(x*(x-1)/2)
    
for i in range(N):
    y=A.count(A[i])
    print(Com-y+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            count += comb(j, 2, exact=True)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.misc import comb
N = int(input())
A = [int(x) for x in input().split()]
C = collections.Counter(A)
def banned_count(seq):
    for i in range(N):
        count = 0
        C[A[i]] -= 1
        for j in C.values():
            count += comb(j, 2, exact=True)
        print(count)
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

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

counter = {}
sum = 0
for b in set(balls):
  n = balls.count(b)
  counter[b] = n
  sum += n * (n-1) // 2

for b in balls:
  n_old = counter[b]
  c_old = n_old * (n_old - 1) // 2
  c_new = (n_old - 1) * (n_old - 2) // 2
  
  result = sum - c_old + c_new
  print(result)

Submission status: Time Limit Exceeded

Solution code:
def combi(n):
  return n*(n-1)//2
 
import sys
input = sys.stdin.readline
N = int(input())
A = input().split()
tmp = 0
idx = list(set(A))
value = [ A.count(_) for _ in idx ]
for n in value:
    tmp += combi(n)
#sum = 0
for i in A:
    j = idx.index(i)
    #sum += tmp - combi(j) + combi(j-1)
    print(int(tmp - combi(value[j]) + combi(value[j]-1)))

Submission status: Time Limit Exceeded

Solution code:
def combi(n):
  return n*(n-1)//2
  
N = int(input())
A = input().split()
tmp = 0
idx = list(set(A))
value = [ A.count(_) for _ in idx ]
for n in value:
    tmp += combi(n)
#sum = 0
for i in A:
    j = idx.index(i)
    #sum += tmp - combi(j) + combi(j-1)
    print(int(tmp - combi(value[j]) + combi(value[j]-1)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [A.count(i+1) for i in range (N)]
C = [int(B[j]*(B[j]-1)/2) for j in range (N)]
D = sum(C)
for k in range(N):
  print(D if B[A[k]-1] < 2 else D-B[A[k]-1]+1)

Submission status: Time Limit Exceeded

Solution code:
def combi(n):
    return n*(n-1)//2

def D159_1():
    N = int(input())
    A = input().split()
    tmp = 0
    idx = list(set(A))
    value = [ A.count(_) for _ in idx ]
    for n in value:
        tmp += combi(n)
    #sum = 0
    for i in A:
        j = idx.index(i)
        #sum += tmp - combi(j) + combi(j-1)
        print(int(tmp - combi(value[j]) + combi(value[j]-1)))


if __name__ == "__main__":
    D159_1()

Submission status: Time Limit Exceeded

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

def calc_n_combinations(l):
  ball_unique = set(l)
  sum = 0
  for b in ball_unique:
    n = l.count(b)
    sum += n * (n-1) // 2
  return sum
 
results = []
c_all = calc_n_combinations(balls)
for b in balls:
  c_tmp = balls.count(b) - 1
  result = c_all - c_tmp
  print(result)

Submission status: Time Limit Exceeded

Solution code:
def combi(n):
    return n*(n-1)/2

def D159_1():
    N = int(input())
    A = input().split()
    tmp = 0
    idx = list(set(A))
    value = [ A.count(_) for _ in idx ]
    for n in value:
        tmp += combi(n)
    #sum = 0
    for i in A:
        j = idx.index(i)
        #sum += tmp - combi(j) + combi(j-1)
        print(int(tmp - combi(value[j]) + combi(value[j]-1)))


if __name__ == "__main__":
    D159_1()

Submission status: Time Limit Exceeded

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

def calc_n_combinations(l):
  ball_unique = set(l)
  sum = 0
  for b in ball_unique:
    n = l.count(b)
    sum += n * (n-1) // 2
  return sum
 
results = []
for i in range(len(balls)):
  tmp = balls.copy()
  tmp.pop(i)
  result = calc_n_combinations(tmp)
  print(result)

Submission status: Time Limit Exceeded

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

p = a
t = sorted(list(set(a)))
for i in range(n):
  x = p[i]
  p[i] = 0
  s = 0
  for j in t:
    m = p.count(j)
    for k in range(m):
      s += k
  print(s)
  p[i] = x

Submission status: Time Limit Exceeded

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

num_cnt = [0]*n
sum_cnt = 0

for num in range(n):
    cnt = a.count(num+1)
    if cnt > 0:
        num_cnt[num] = cnt
        sum_cnt += cnt*(cnt-1)//2
res = [0]*n

for num in range(n):
    cnt = num_cnt[num]
    res[num] = (sum_cnt - (cnt-1)*(cnt - (cnt-2))//2) if cnt != 0 else 0

for a_k in a:
    print(res[a_k-1])

Submission status: Time Limit Exceeded

Solution code:
# ncr再びって感じ(Aもncrだった)
# 登場回数が2以上のものが対象になる 多そうなのでncr自体をメモ化しておくか
# TLEくらった もうすこしメモ化してみる（どうせ同じ数があるのでncrの計算を減らすべく、答え自体をメモ化した(memo2)）

n = int(input())
arr = list(map(int, input().split()))
memo = {}
memo2 = {}
dic = {}
for a in arr:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1


def ncr(n, r):
    # ささやかなメモ化
    if (n, r) in memo:
        return memo[(n, r)]
    res = 1
    for i in range(1, r + 1):
        res = res * (n - i + 1) // i
    memo[(n, r)] = res
    return res


for a in arr:
    if a in memo2:
        print(memo2[a])
    else:
        ans = 0
        dic[a] -= 1
        for k, v in dic.items():
            if v > 1:
                ans += ncr(v, 2)
        print(ans)
        dic[a] += 1
        memo2[a] = ans

Submission status: Time Limit Exceeded

Solution code:
# ncr再びって感じ(Aもncrだった)
# 登場回数が2以上のものが対象になる 多そうなのでncr自体をメモ化しておくか

n = int(input())
arr = list(map(int, input().split()))
memo = {}
dic = {}
for a in arr:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1


def ncr(n, r):
    # ささやかなメモ化
    if (n, r) in memo:
        return memo[(n, r)]
    res = 1
    for i in range(1, r + 1):
        res = res * (n - i + 1) // i
    memo[(n, r)] = res
    return res


for a in arr:
    ans = 0
    dic[a] -= 1
    for k, v in dic.items():
        if v > 1:
            ans += ncr(v, 2)
    print(ans)
    dic[a] += 1

Submission status: Time Limit Exceeded

Solution code:
import itertools 

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

for k in range(N):
    A_replace = A[:]
   
    del A_replace[k]
    cnt = 0 
    for i in set(A_replace):
        cnt += ((A_replace.count(i) * (A_replace.count(i)-1)) / 2)
    A_replace = A
    print(int(cnt))