Educational Codeforces Round 115 (Rated for Div. 2) C. Delete Two Elements

C. Delete Two Elements

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Monocarp has got an array a consisting of n integers. Let's denote k as the mathematic mean of these elements (note that it's possible that k is not an integer).

The mathematic mean of an array of n elements is the sum of elements divided by the number of these elements (i. e. sum divided by n).

Monocarp wants to delete exactly two elements from a so that the mathematic mean of the remaining (n−2) elements is still equal to k.

Your task is to calculate the number of pairs of positions [i,j] (i<j) such that if the elements on these positions are deleted, the mathematic mean of (n−2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a).

Input

The first line contains a single integer t (1≤t≤1e4) — the number of testcases.

The first line of each testcase contains one integer n (3≤n≤2⋅1e5) — the number of elements in the array.

The second line contains a sequence of integers a1,a2,…,an (0≤ai≤1e9), where ai is the i-th element of the array.

The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.

Output

Print one integer — the number of pairs of positions [i,j] (i<j) such that if the elements on these positions are deleted, the mathematic mean of (n−2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a).

Example

input

4
4
8 8 8 8
3
50 20 10
5
1 4 7 3 5
7
1 2 3 4 5 6 7

output

6
0
2
3

Note

In the first example, any pair of elements can be removed since all of them are equal.

In the second example, there is no way to delete two elements so the mathematic mean doesn't change.

In the third example, it is possible to delete the elements on positions 1 and 3, or the elements on positions 4 and 5.

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		vector<int> a(n);
		long long sum = 0;
		for(int i = 0;i < n;i++){
			cin >> a[i];
			sum += a[i];
		}
		long long ans = 0;
		map<long long ,long long> mp;
		for(int i = 0;i < n;i++){
			ans += mp[1ll * n * a[i]];
			mp[2ll * sum - 1ll * n * a[i]]++;
//例三:1 4 7 3 5 sum=20 ->5 20 35 15 25  平均数20
//ans+=mp[5],mp[35]=1
//ans+=mp[20],mp[20]=1
//ans+=mp[35],mp[5]=1
//ans+=mp[15],mp[25]=1
//ans+=mp[25],mp[15]=1
//原平均数sum/n,要求删除的两个数需要是2sum/n,避免小数,所有数*n
//平均数变成sum,那么删除的两个数的和是2sum
//用map记录 
		}
		cout << ans << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值