HDU_5528_Count a * B, hdu_5528_count
Count a * B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission (s): 872 Accepted Submission (s): 315
Problem DescriptionMarry likes to count the number of ways to choose two non-negative integers a and B less than m to make a × B mod m = 0.
Let's denote f (m) as the number of ways to choose two non-negative integers a and B less than m to make a x B mod m = 0.
She has calculated a lot of f (m) for different m, and now she is interested in another function g (n) = Σ m | nf (m ). for example, g (6) = f (1) + f (2) + f (3) + f (6) = 0 + 1 + 4 + 21 = 26. she needs you to double check the answer.
GB you n. Your task is to find g (n) modulo 264.
InputThe first line contains an integer T indicating the total number of test cases. Each test case is a line with a positive integer n.
1 ≤ T ≤20000
1 ≤ n ≤109
OutputFor each test case, print one integer s, representing g (n) modulo 264.
Sample Input26514
Sample Output26328194
Source2015ACM/ICPC Asia Changchun station-reproduction competition (thanks to Northeast China Normal University)
- To push the formula, paste the process:
- Note:
- (1) The second half of the formula can be regarded as the Division of variable a in the integer sense, and then the gcd result is added. There is room for improvement in the way variables are divided based on gcd results. Considering gcd (x, a) | x, we use the x Factor to divide gcd results, and it is best to use the Dirichlet form for improvement. After all, gcd and common functions are both product functions, new functions in the Dirichlet form maintain the property of the product functions, facilitating computation.
- (2) For the simplification result of f (x), it is possible to take g (n) into consideration for further simplification.
- (3) considering the transfer of division, it is best to delete the variable x here. Let's enumerate d first, find x again, and then replace x/d with I, I * d | n, where I * d = x, according to the Division nature, it is not difficult to obtain I | (d/n). Then we can see a very familiar formula. The second half formula is the Euler function for finding all the factors of n, and the result is n itself.
- (4) Final Result
- (5) There is a certain benefit in understanding k times of a factor as polynomial multiplication. The advantage is that there is no division processing, and all of them are simple addition and multiplication, this is a good news for modulo operations. In addition, considering that the k-power function is a product function, you can first decompose the prime factor of n and then solve it step by step. However, here we need to perform an equal-ratio Summation for the multiplication of each prime factor, involving division, the corresponding anti-overflow operation should be performed in the Code (well, I won't perform this QAQ)
- As for the question, the 2 ^ 64 modulo operation is OK under unsigned longlong.
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue>10 #include <stack>11 #include <set>12 #include <map>13 using namespace std;14 typedef long long LL ;15 typedef unsigned long long ULL ;16 const int maxn = 1e5 + 10 ;17 const int inf = 0x3f3f3f3f ;18 const int npos = -1 ;19 const int mod = 1e9 + 7 ;20 const int mxx = 100 + 5 ;21 const double eps = 1e-6 ;22 const double PI = acos(-1.0) ;23 24 ULL T, n, prime[maxn], nd, theta2;25 bool vis[maxn];26 int tot, cnt;27 void init(int top){28 tot=0;29 memset(vis,true,sizeof(vis));30 for(int i=2;i<=top;i++){31 if(vis[i]){32 prime[tot++]=(ULL)i;33 }34 for(int j=0;j<tot&&(i*prime[j]<=top);j++){35 vis[i*prime[j]]=false;36 if(i%prime[j]==0)37 break;38 }39 }40 }41 int main(){42 // freopen("in.txt","r",stdin);43 // freopen("out.txt","w",stdout);44 init(1e5+1);45 while(~scanf("%llu",&T)){46 while(T--){47 scanf("%llu",&n);48 theta2=1ULL;49 nd=n;50 for(int i=0;i<tot && prime[i]*prime[i]<=n;i++)51 if(n%prime[i]==0){52 ULL p=prime[i], alpha=0ULL;53 ULL sigma=1ULL, square=1ULL;54 while(n%p==0ULL){55 alpha++;56 square*=p;57 sigma+=square*square;58 n/=p;59 }60 nd*=alpha+1ULL;61 theta2*=sigma;62 }63 if(n>1){64 nd*=2ULL;65 theta2*=(1ULL+n*n);66 }67 printf("%llu\n",theta2-nd);68 }69 }70 return 0;71 }