The Boss on Mars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1649 Accepted Submission(s): 492
Problem Description
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss.
Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.
Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.
Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.
Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.
Input
The first line contains an integer T indicating the number of test cases. (1 ≤ T ≤ 1000) Each test case, there is only one integer n, indicating the number of employees in ACM. (1 ≤ n ≤ 10^8)
Output
For each test case, output an integer indicating the money the boss can save. Because the answer is so large, please module the answer with 1,000,000,007.
Sample Input
2 4 5
Sample Output
82 354HintCase1: sum=1+3*3*3*3=82 Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
首先,四次方和不好算,不可能预处理每个数的4次方,空间和时间都不允许。
不过,1^4 + 2^4 + ... + n^4 这样的式子还是有公式的。其结果为 n(n+1)(2n+1)(3n^2+3n+1)/30。
给定的n范围为10^8,最终的结果模10^9+7。这个公式由于除30的原因,不能直接进行计算。我们需要求出30在模10^9+7(素数)系里的逆元。
笔者直接用exgcd计算,得到的结果是233333335。这样我们就可以求出任意一个 n 四次方和模10^9+7的值。
题目中求互质的数,那么我们可以求出非互质的数,即与n的约数大于1的数,的四次方和,用n的四次方和减去就好了。
假定n存在2这样的素因子,那么n与2,4,6,8。。。这样的数都不互质。我们可以求出2,4,6,8,。。。的四次方和。
简单观察,可以发现,2^4+4^4+6^4+8^4+... = 2^4 * ( 1^4 + 2^4 + 3^4 + 4^4... ),仍然可以使用公式计算。
最后,我们对n所有的素因子都进行这样的处理。利用容斥原理去重,即可得到本题的解。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod = 1000000007;
const int inv_30 = 233333335;
typedef long long LL;
int cas;
int prime[30];
int top;
int func(LL n)
{
return n*(n+1)%mod*(2*n+1)%mod*(3*(n*n)%mod+3*n-1)%mod*inv_30%mod;
}
void work()
{
int n;
scanf("%d", &n);
int nn = n;
top = 0;
for(int i=2;i*i<=nn;i++) if(nn%i==0)
{
prime[top++] = i;
while(nn%i==0) nn/=i;
}
if(nn>1) prime[top++] = nn;
int res = func(n);
for(int i=1;i<(1<<top);i++)
{
int tmp=1,flag=false;
for(int j=0;j<top;j++) if(i&(1<<j))
tmp*=prime[j], flag = !flag;
int t = func(n/tmp);
tmp = ((LL)tmp*tmp)%mod;
tmp = ((LL)tmp*tmp)%mod;
tmp = ((LL)tmp*t)%mod;
if(flag)
res = (res-tmp)%mod;
else
res = (res+tmp)%mod;
}
printf("%d\n", (res+mod)%mod);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
work();
}