Arkady and his friends love playing checkers on an n×nn×n field. The rows and the columns of the field are enumerated from 11 to nn.
The friends have recently won a championship, so Arkady wants to please them with some candies. Remembering an old parable (but not its moral), Arkady wants to give to his friends one set of candies per each cell of the field: the set of candies for cell (i,j)(i,j) will have exactly (i2+j2)(i2+j2) candies of unique type.
There are mm friends who deserve the present. How many of these n×nn×n sets of candies can be split equally into mm parts without cutting a candy into pieces? Note that each set has to be split independently since the types of candies in different sets are different.
Input
The only line contains two integers nn and mm (1≤n≤1091≤n≤109, 1≤m≤10001≤m≤1000) — the size of the field and the number of parts to split the sets into.
Output
Print a single integer — the number of sets that can be split equally.
Examples
3 3
1
6 5
13
1000000000 1
1000000000000000000
Note
In the first example, only the set for cell (3,3)(3,3) can be split equally (32+32=1832+32=18, which is divisible by m=3m=3).
In the second example, the sets for the following cells can be divided equally:
- (1,2)(1,2) and (2,1)(2,1), since 12+22=512+22=5, which is divisible by 55;
- (1,3)(1,3) and (3,1)(3,1);
- (2,4)(2,4) and (4,2)(4,2);
- (2,6)(2,6) and (6,2)(6,2);
- (3,4)(3,4) and (4,3)(4,3);
- (3,6)(3,6) and (6,3)(6,3);
- (5,5)(5,5).
In the third example, sets in all cells can be divided equally, since m=1m=1.
题意:
给定一个n和一个数m,求有多少对数a,b,满足一下条件:
1<=a<=n
1<=b<=n
(a*a+b*b)%m==0
思路:
这题主要考察了取余的性质。
我们应该知道 (a+b)%M=(a%M+b%M)%M
(a*b)%m=((a%m)*(b%m))%m
知道这个性质的话,我们就可以把(a*a+b*b)%m==0
转为(a%m*a%m+b%m*b%m)%m==0
因为a%m和b%m的范围是0~m-1,
那么我们可以先预处理出1~n中,有多少个数对m取余的结果是i,0<=i<=m-1
然后m*m的时间复杂度去枚举 i*i+j*j 是否是m的倍数,如果是,答案加上 i的数量乘以j的数量。
细节见代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ ll cnt[maxn]; int main() { //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); ll n; ll k; ll ans=0ll; cin>>n>>k; repd(i,0,k-1) { cnt[i]=n/k; if((n%k)>=i) { cnt[i]++; } } cnt[0]--; repd(i,0,k-1) { repd(j,0,k-1) { if((i*i+j*j)%k==0) { ans+=cnt[i]*cnt[j]; } } } cout<<ans<<endl; return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }