CSM
初看这题,觉得应用2重循环枚举(超时)。
(又是数据提醒),考虑用O(n)来做(将式子转化为只含i的)。
(ai + aj) ∗ (ai2 + aj2) ≡ k mod p ->
(ai + aj) * (ai - aj) * (ai2 + aj2) ≡ k * (ai - aj) mod p ->
ai4 - k * ai ≡ aj4 - k * ai mod p
用这个式子来码,就行了。
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 300002;
int n;
ll p, k, a[N], tmp = -1, ans, tot;
int main() {
cin >> n >> p >> k;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
a[i] = (a[i] * a[i] % p * a[i] % p * a[i] % p - (k * a[i]) % p + p) % p;
}
sort(a + 1, a + n + 1);
a[n + 1] = -1;
for(int i = 1; i <= n + 1; i ++)
if(tmp == a[i])
tot ++;
else {
if(tmp != -1)
ans = (ans + tot * (tot - 1) / 2) % p;
tmp = a[i];
tot = 1;
}
cout << ans << endl;
return 0;
}
注:不能用pow会爆!
如果有错误,请大佬指出,谢谢!