题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4059
题意:求1到n中所有与n互质的数的四次方之和。
思路:
可以通过用所有数的四次方和减去不与n互质的数的四次方来得到答案。不与n互质的数用容斥定理来计算,比如当前减去x的倍数的四次方,那么就是x^4+(2*x)^4+...(n/x *x)^4,
我们将x^4提出来,就是x^4*(1^4+2^4+3^4+....n/x ^4)。取模除法用逆元来解决,注意取模的位置,不要让局部数据溢出。
四次方求和公式:1^4+2^4+...n^4 = n(n+1)(2n+1)(3n^2+3n-1)/30
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 1000000007
int T;
int n;
int m;
int prime[101];
LL ans;
LL niyuan;
void init(int x)
{
m = 0;
for(int i = 2; i*i <= x; i++)
{
if ( x % i == 0 )
{
prime[++m] = i;
while( x % i == 0 ) x/=i;
}
}
if ( x > 1 ) prime[++m] = x;
}
LL Pow(int a , int b)
{
LL ans = 1;
LL temp = a;
while( b )
{
if ( b & 1 ) ans = (ans*temp)%mod;
temp = (temp*temp)%mod;
b>>=1;
}
return ans;
}
LL calcu( int k )
{
LL ans = 1;
ans = (ans*k*(k+1))%mod;
ans = (ans* ((2*k+1)%mod) )%mod;
ans = (ans* ( ((LL)k*k*3)%mod + (3*k-1)%mod )%mod )%mod;
ans = ( ans * niyuan )%mod;
return ans;
}
LL f(int x)
{
LL ans = 1;
ans = (ans*x)%mod;
ans = (ans*x)%mod;
ans = (ans*x)%mod;
ans = (ans*x)%mod;
return ans;
}
int main()
{
niyuan = Pow(30,mod-2);
cin>>T;
while(T--)
{
scanf("%d",&n);
init(n);
ans = calcu(n);
int uplim = 1<<m;
rep(i,1,uplim-1)
{
int cnt = 1;
int num = 0;
rep(j,0,m-1)
if ( i & (1<<j) )
{
num++;
cnt*=prime[j+1];
}
LL temp = ( calcu(n/cnt) * f(cnt) )%mod;
if ( num & 1 ) ans = ( ( ans - temp)%mod+mod )%mod;
else ans = (ans+temp)%mod;
}
cout<<ans<<endl;
}
return 0;
}