传送门
2
5 7
1 2 3 4 5
6 7
1 2 3 4 5 6
4
6
题目大意:
有 n 个小于质数 p 的非负整数 a1…n ,你想知道有多少对 i,j(1≤i<j≤n) ,使得模 p 意义下 1ai+aj≡1ai+1aj ,即这两数的和的逆元等于它们逆元的和,注意零元没有逆元。 1≤n≤105,2≤p≤1018 。
解题思路:
代码:
#include
using namespace std;
typedef long long LL;
const int MAXN = 1e5+5;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
inline int GCD(int a, int b){
if(b == 0) return a;
return GCD(b, a%b);
}
inline LL GCD(LL a, LL b){
if(b == 0) return a;
return GCD(b, a%b);
}
inline int LCM(int a, int b){
return a/GCD(b, a%b)*b;
}
inline LL LCM(LL a, LL b){
return a/GCD(b, a%b)*b;
}
namespace IO {
const int MX = 4e7; //1e7占用内存11000kb
char buf[MX]; int c, sz;
void begin() {
c = 0;
sz = fread(buf, 1, MX, stdin);
}
inline bool read(LL &t) {
while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
if(c >= sz) return false;
bool flag = 0; if(buf[c] == '-') flag = 1, c++;
for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
if(flag) t = -t;
return true;
}
}
LL Multi(LL a, LL b, LL p){
LL ans = 0;
while(b){
if(b & 1) ans = (ans + a) % p;
b>>=1;
a = (a + a) % p;
}
return ans;
}
LL Pow(LL a, LL b, LL p){
LL ans = 1;
while(b){
if(b & 1) ans = Multi(ans, a, p);
b>>=1;
a = Multi(a, a, p);
}
return ans;
}
void Exgcd(LL a, LL b, LL &x, LL &y){
if(b == 0){
x = 1;
y = 0;
return ;
}
LL x1, y1;
Exgcd(b, a%b, x1, y1);
x = y1;
y = x1 - (a/b)*y1;
}
mapint >mp, mp1;
mapint >::iterator it;
int main(){
//freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
//freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
IO::begin();
LL T; //scanf("%d", &T);
IO::read(T);
while(T--){
LL n; //scanf("%d", &n);
IO::read(n);
LL p, x; //scanf("%lld", &p);
IO::read(p);
mp.clear();
mp1.clear();
for(int i=0; i//scanf("%lld",&x);
IO::read(x);
if(x == 0) continue;
mp1[x]++;
x = Pow(x, 3, p);
mp[x]++;
}
LL ans = 0;
for(it=mp.begin(); it!=mp.end(); it++){
LL tmp = (it->second);
tmp = tmp*(tmp-1)/2;
ans = ans + tmp;
}
if(p != 3)
for(it=mp1.begin(); it!=mp1.end(); it++){
LL tmp = (it->second);
tmp = tmp*(tmp-1)/2;
ans = ans - tmp;
}
printf("%lld\n",ans);
}
return 0;
}
/**
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 每次AC
**/