ZOJ - 4101 - Element Swapping (数学)
题目大意: 有一个n个数的序列,交换其中两个Ai与Aj,给你交换前后的两种关系。 问有多少种情况。
题解 : https://blog.csdn.net/u011815404/article/details/89607866 (写的很好! 懒得写公式了。。。.)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5+5; int T, n; ll a[maxn]; ll x, y, sum; map<int, int> cnt; int main() { scanf("%d",&T); while(T--) { cnt.clear(); sum = 0; scanf("%d%lld%lld",&n, &x, &y); for(int i = 1; i <= n; i++) { scanf("%lld",&a[i]); cnt[a[i]]++; x -= 1ll * i * a[i]; y -= 1ll * i * a[i] * a[i]; } if(x == 0) { if(y != 0) { printf("0\n"); continue; } for(int i = 1; i <= n; i++) sum += cnt[a[i]]-1; } else { if(y % x != 0) { printf("0\n"); continue; } else { ll num = y / x; for(int i = 1; i <= n; i++) { ll Aj = num - a[i]; // cout << "Aj = " << Aj << endl; if(Aj == a[i]) continue; ll j = (x-Aj*i+a[i]*i)/(a[i]-Aj); // cout << "j = " << j << endl; if(j < 0) continue; if(a[j] == Aj) sum++; } } } printf("%lld\n",sum/2); } return 0; }