Codeforces 1188B 式子转化

思路:看到(a + b)想到乘上(a - b)变成平方差展开(并没有想到2333), 两边同时乘上a - b, 最后式子转化成了a ^ 4 - ka = b ^ 4 - kb,剩下的就水到渠成了。

0的时候特判一下即可。

代码:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define pii pair<int, int>
#define db double
using namespace std;
const int maxn = 100010;
map<int, int> mp;
int mod, n;
int qpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1) {
		if(y & 1) ans = ((LL)ans * x) % mod;
		x = ((LL)x * x) % mod;
	}
	return ans;
}
map<int, int>::iterator it;
int main() {
	int x, k;
	LL ans = 0;
	scanf("%d%d%d", &n, &mod, &k);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &x);
		x = (qpow(x, 4) - ((LL)k * x) % mod + mod) % mod;
		mp[x]++;
	}
	for (it = mp.begin(); it != mp.end(); it++) {
		LL tmp = it -> second;
		ans += tmp * (tmp - 1) / 2;
	}
	if(mod == 0) {
		LL tmp = mp[0];
		ans += tmp * (tmp - 1) / 2;
	}
	printf("%lld
", ans);
}

  

原文地址:https://www.cnblogs.com/pkgunboat/p/11144104.html