题意

题目链接

求满足\(i^2 + j^2 \% M = 0\)的数对\((i, j)\)的个数,\(1 \leqslant i, j \leqslant 10^9, M \leqslant 1000\)

Sol

发这篇博客的目的就是为了证明一下我到底有多菜。

mdzz小学组水题我想了40min都没想出来。这要是出在NOIP 2019的话估计我又要做不出Day1T1了。。

\(i^2 + j^2 \% M = i \% M * i \% M + j \% M * j \% M\)

枚举剩余系即可

此题完结

/*
*/
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define rg register
#define pt(x) printf("%d ", x);
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, INF = 1e9 + 10, mod = 1e9 + 7;
const double eps = 1e-9;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, vis[3001][3001];
int get(int l, int r) {
return (r - l) / M + (l > 0);
}
main() {
N = read(); M = read();
int ans = 0;
for(int i = 1; i <= M; i++) {
for(int j = 1; j <= M; j++) {
if((i * i + j * j) % M == 0) {
vis[i % M][j % M] = 1;
}
}
}
for(int i = 0; i <= min(N, M); i++)
for(int j = 0; j <= min(N, M); j++)
if(vis[i][j] && ((i * i + j * j) % M == 0))
(ans += get(i, N) * get(j, N));
cout << ans;
return 0;
}
/* */
05-11 16:28