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.
2 4 5
82
354
Case1: sum=1+3*3*3*3=82 Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
题意是:给出n让求1~n中与n互素的数的4次方之和,结果模1,000,000,007
这里可以先求出总和再减去不互素的,因为可以通过对n分解质因数,快速找到与n不互素的数,
这里减去不互素额的方法就是利用容斥定理:先减去所有质因子的4次方和,再加上是两个质因子的倍数的4次方和(显然这里质因子之间是互素的,所以最小公倍数即是两者之积),再减去三个质因子的倍数的4次方之和……
这里容斥定理用位运算和dfs均可
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define mod 1000000007vector<ll>prime;
ll len,n,ans;ll pow_mod(ll n,ll k)
{ll res=1;while(k>0){if(k&1)res=res*n%mod;n=n*n%mod;k>>=1;}return res;
}ll sum(ll x) //求1~n的4次方之和余mod,用到除法求逆元
{ll t1 = (x * (x + 1) % mod) * ((2 * x + 1) % mod);ll t2 = ((3 * x * x + 3 * x - 1) % mod) * pow_mod(30ll, mod - 2);return ((t1 % mod) * (t2 % mod)) % mod;
}ll solve() //位运算解决
{ll ans=0;for(ll msk=1;msk<(1ll<<len);msk++){ll bits=0,cnt=1;for(ll i=0;i<len;i++){if(msk&(1<<i)){bits++;cnt*=prime[i];}}if(bits&1){ans+=pow_mod(cnt,4)*sum(n/cnt); //pow_mod(cnt,4)*sum(n/cnt)是所有cnt的倍数的四次方和if(ans>=mod)ans%=mod;}else{ans-=pow_mod(cnt,4)*sum(n/cnt);if(ans>=mod)ans%=mod;}}return ans;
}/*void dfs(ll pos,ll pre,int sgin) //dfs解决
{if(pos>=len||pre>n)return;ll t=1,cur=pre*prime[pos];for(int i=0;i<4;i++)t=(t*cur)%mod;t=(t*sum(n/cur))%mod;ans=((ans%mod)+(sgin*t)%mod+mod)%mod;if(n%cur==0)dfs(pos+1,cur,-sgin);dfs(pos+1,pre,sgin);
}*/int main()
{int T;ll i;scanf("%d",&T);while(T--){scanf("%lld",&n);prime.clear();//分解质因数ll tn=n;for(i=2;i*i<=tn;i++){if(tn%i==0){prime.push_back(i);while(tn%i==0)tn=tn/i;}}if(tn!=1)prime.push_back(tn);len=prime.size();ans=sum(n);//dfs(0,1,-1);ans=((ans-solve())%mod+mod)%mod;printf("%lld\n",ans);}return 0;
}
参考:求1~n中与n互素的数的4次方之和