B.Element Swapping
这道题目,我看错了题意,我以为是交换多次,实际上是交换一次。
我们先设 x = ai*i+aj*j, x' = aj*i+ai*j
则有x - x' = (i-j)*(ai-aj)
同理,
y - y' = (i-j)*(ai^2-aj^2)
两个相除(假设分母不为0),得到
(y-y')/(x-x') = ai+aj
PS:
1.注意分类讨论0的情况
2.由于x、y可能是任意给出的,意味着x,y不一定是正确的。因此还要建立线性约束条件,其中有四条约束条件:
条件一:(y-y')/(x-x') 不等于2*a[i]
条件二:(x-x') 能整除(y-y')/(x-x') - 2*[i]
条件三:i + (x-x')/((y-y')/(x-x')-2*a[i]) 在【1,n】内
条件四:a[i] + a【i + (x-x')/((y-y')/(x-x')-2*a[i]) 】 = (y-y')/(x-x')
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 20;
typedef long long ll;
ll x,y,x2,y2,ans;
int a[N];
int num[N];
bool vis[N];
int main()
{
ios_base::sync_with_stdio(false);
int t,n;
cin >> t;
while(t--){
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
ans = x2 = y2 = 0;
cin >> n >> x >> y;
for(int i = 1;i <= n;++ i){
cin >> a[i];
num[a[i]] ++;
x2 += i * a[i];
y2 += i * a[i] * a[i];
}
y2 = y2 - y;
x2 = x2 - x;
if(x2 == 0 && y2 == 0){
for(int i = 1;i <= n;++ i){
if(vis[a[i]] == 1)
continue;
vis[a[i]] ++;
ans += (ll)num[a[i]]*(num[a[i]]-1)/2;
}
cout << ans << endl;
}
else if(x2 && y2){
if(y2%x2){
cout << 0 << endl;
continue;
}
else{
ll tmp = y2/x2;
for(int i = 1;i <= n;++ i){
ll p = tmp - 2 * a[i];
ll q = x2 / p;
ll z = i + q;
if(p == 0) continue;
if(x2 % p) continue;
if(z >= 1 && z <= n)
if(a[i] + a[z] == tmp)
ans ++;
}
cout << ans/2 << endl;
}
}
else if(x2 == 0 && y2){
cout << 0 << endl;
continue;
}
}
}