题意:
求出1~n的与n互质的数的4次方的和
思路:
F(i)表示1~i的4次方的和
Fac[i]表示n的质因子
Ans=F(n)-Fac(i)^4*F(n/Fac(i))+(Fac(i)+Fac(j))^4*F(n/(Fac(i)*Fac(j)))….
奇加偶减
F(i)=n(1 + n)(1 + 2 n)(-1 + 3 n + 3 n^2)/30
因为会边除边取模,所以我们取模的时候乘以30对MOD的逆元然后取模
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
typedef long long LL;
#define maxn 10005
const int mod=1000000007;
const int inf=(1<<28)-1;
int fac[maxn],len;
void get_fac(LL x)
{
int t=x;
len=0;
for(int i=2;i*i<=t;++i)
if(t%i==0)
{
fac[++len]=i;
while(t%i==0) t/=i;
}
if(t>1) fac[++len]=t;
}
LL ans,n;
LL mul(LL a,LL b)
{
return (a*b)%mod;
}
LL mul_1(LL a)
{
return mul(a,mul(a,mul(a,a)));
}
LL mul_2(LL a)
{
return mul(a,mul(1+a,mul(1+2*a,mul((mul(3,a)+mul(3,mul(a,a))-1+mod)%mod,233333335))));
}
LL mul_3(LL a)
{
return (mul_1(a)*(mul_2(n/a)))%mod;
}
//pow(1~n,4)=n(1 + n)(1 + 2 n)(-1 + 3 n + 3 n^2)/30
void dfs(int pos,int tot,LL val)
{
if(val>n) return ;
if(pos==len+1)
{
if(!tot) return ;
if(tot&1) ans=(ans+mul_3(val))%mod;
else ans=((ans-mul_3(val))%mod+mod)%mod;
return ;
}
dfs(pos+1,tot+1,val*fac[pos]);
dfs(pos+1,tot,val);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
get_fac(n);
ans=0;
dfs(1,0,1);
ans=(mul_2(n)-ans+mod)%mod;
printf("%lld\n",ans);
}
return 0;
}