hdu4059 The Boss on Mars(容斥)

来源:互联网 发布:pageoffice java word 编辑:程序博客网 时间:2024/05/19 04:05

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
245
Sample Output
82354          
Hint
Case1: sum=1+3*3*3*3=82Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354        

看了样例,题意应该比较好理解

需补充的知识点

1)1^4+2^4+3^4+……n^4=(6n^5+15n^4+10n^3-n)/30

2)逆元的知识

3)求解质因数

为啥用容斥呢?

对于样例1,输入4

可知2为质因数,由于不互质较难求得,我们求与n互质的

那么2为质因数,2的任意倍数都会被除去,且这些倍数的四次方和为2(1^4+2^4+……+(n/2)^4)

若质因数包括3,3的倍数也会被除去

但是2,3有公共倍数,需要用容斥处理。

容斥有三种实现方式,队列数组,dfs,二进制模拟,

我喜欢用队列数组

#include <iostream>using namespace std;typedef long long ll;const ll maxn=1e6;const ll mod=1000000007;ll prime[maxn];ll fac[maxn];ll flag[maxn];ll fast_pow(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans;}ll sum_n(ll n){ll ans;ans=((6*fast_pow(n,5)%mod+15*fast_pow(n,4)%mod+10*fast_pow(n,3)%mod-n+mod)%mod)*fast_pow(30,mod-2)%mod;return ans;}ll n_4(ll n){return fast_pow(n,4);}void solve(ll n){ll temp=n;ll num=0;for(int i=2;i*i<=n;i++){if(temp%i==0){prime[num++]=i;while(temp%i==0){temp/=i;}}}if(temp!=1)prime[num++]=temp;ll cnt=0;fac[cnt]=1;flag[cnt++]=1;for(int i=0;i<num;i++){ll temp_cnt=cnt;for(int j=0;j<temp_cnt;j++){fac[cnt]=fac[j]*prime[i];flag[cnt++]=flag[j]*-1;}}ll ans=0;for(int i=0;i<cnt;i++){ans=(ans+((flag[i]*n_4(fac[i]))%mod)*sum_n(n/fac[i])%mod+mod)%mod;}cout<<ans<<endl;}int main(){ll t;cin>>t;while(t--){ll n;cin>>n;solve(n);}return 0;}