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.

容斥原理

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<math.h>
 5 using namespace std;
 6 typedef long long ll;
 7 
 8 const int mod=1e9+7;
 9 
10 ll QP(ll a,ll n){
11     ll ans=1,tmp=a;
12     while(n){
13         if(n&1)ans=ans*tmp%mod;
14         tmp=tmp*tmp%mod;
15         n>>=1;
16     }
17     return ans;
18 }
19 
20 ll getsum(ll n){
21     return n*(n+1)%mod*(2*n+1)%mod*((n*n*3%mod+n*3%mod-1+mod)%mod)%mod*QP(30,mod-2)%mod;
22 }
23 
24 int pnum[50],num;
25 
26 int main(){
27     int T;
28     scanf("%d",&T);
29     while(T--){
30         int n;
31         scanf("%d",&n);
32         num=0;
33         int tmp=n;
34         for(int i=2;i*(ll)i<=tmp;++i){
35             if(!(tmp%i)){
36                 pnum[++num]=i;
37                 while(!(tmp%i))tmp/=i;
38             }
39         }
40         if(tmp-1)pnum[++num]=tmp;
41         ll ans=0;
42         for(int i=1;i<(1<<num);++i){
43             int bit=0;
44             int mul=1;
45             for(int j=1;j<=num;++j){
46                 if(i&(1<<(j-1))){
47                     bit++;
48                     mul*=pnum[j];
49                 }
50             }
51             if(bit%2)ans=(ans+QP(mul,4)*getsum(n/mul)%mod)%mod;
52             else ans=(ans-QP(mul,4)*getsum(n/mul)%mod)%mod;
53         }
54         printf("%lld\n",((getsum(n)-ans)%mod+mod)%mod);
55     }
56     return 0;
57 }
View Code

 

转载于:https://www.cnblogs.com/cenariusxz/p/6597964.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值