題意 "題目鏈接" 求滿足$i^2 + j^2 \% M = 0$的數對$(i, j)$的個數,$1 \leqslant i, j \leqslant 10^9, M \leqslant 1000$ Sol 發這篇博客的目的就是為了證明一下我到底有多菜。 mdzz小學組水題我想了40min都沒想出來 ...
題意
求滿足\(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;
}
/*
*/