hdu4059 The Boss on Mars(容斥)

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
2
4
5
Sample Output
82
354

        
  
Hint
Case1: sum=1+3*3*3*3=82
Case2: 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值