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.
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
Sample Output
思路: 求1~n内与n互素的四次方之和,可以先利用公式求1~n的四次方和:1^4+2^4+3^4+......+n^4=n(n+1)(2n+1)(3n^2+3n-1)/30;然后在减去与n不互素的四次方之和(用容斥),这里要注意的是,因为n非常大,所以,直接除以30会导致答案错误,这时候就要用到逆元中的推论,设x为b/a模n的除数,若gcd(a,n)=1,则x=b/a (mod n)的解为x=b*a^(phi(n)-1);因为30^(phi(l)-1)%l=233333335,所以计算的时候可以把除以30转化为乘以233333335;计算的时候与x不互素的个数d=n/s;这时候又是一个1~d的四次方和公式;
代码如下:
#include<stdio.h>
#include<math.h>
long long p[10000],L=1000000007,m=233333335;
long long pp(int x)
{
long long s=1;;
for(int i=1;i<=4;i++)
s=s*x%L;
return s;
}
long long QH(long long n)
{
return n*(n+1)%L*(2*n+1)%L*(3*(n*n)%L+3*n-1)%L*m%L;
}
int main()
{
int t;
long long n,w,x,S,cnt,y;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%lld",&n);
S=QH(n);
cnt=0;
x=n;
for(int j=2;j*j<=x;j++)
{
if(x%j==0)
{
p[cnt++]=j;
while(x%j==0)
x=x/j;
}
}
if(x>1)
p[cnt++]=x;
for(int j=1;j<(1<<cnt);j++)
{
long long s=1,sum=0;
for(int k=0;k<cnt;k++)
{
if(j&(1<<k))
{
sum++;
s=s*p[k];
}
}
s=pp(s)*QH(n/s)%L;
if(sum%2)
S=(S-s)%L;
else
S=(S+s)%L;
}
printf("%lld\n",(S+L)%L);
}
return 0;
}