cf1056B. Divide Candies(数论 剩余系)
程序员文章站
2023-09-05 18:37:27
题意 "题目链接" 求满足$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; } /* */