zoukankan      html  css  js  c++  java
  • 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
    ",((getsum(n)-ans)%mod+mod)%mod);
    55     }
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    学习vue_01
    练习题 vue_01:
    测试
    django小结
    BBS_02day
    BBS 03day
    力扣(LeetCode)412. Fizz Buzz
    力扣(LeetCode)415. 字符串相加
    力扣(LeetCode)448. 找到所有数组中消失的数字
    力扣(LeetCode)453. 最小移动次数使数组元素相等
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6597964.html
Copyright © 2011-2022 走看看