HDU 4059 The Boss on Mars 容斥原理

The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1528    Accepted Submission(s): 452


Problem Description
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
 

 

Author
ZHANG, Chao
 

 

Source
2011 Asia Dalian Regional Contest
 
     我喜欢非递归写法,即直接用二进制数去模拟。
   

观察到与n互质的数的性质

 

比如12=2*2*3

那么与12不互质的数就有2,3,4,6,8,9,10,12

其实就是2的所有倍数,以及3的所有倍数

 

所以可以先求一个1到12的所有数的四次方和。这个有公式:

n*(n+1)*(6*n*n*n+9*n*n+n-1)/30(先开始就是抄错了。。)

注意这里30最好求一下逆元大概是2000多万

求的所有的四次方和之后当然要减去那些不互质的数的四次方

也就是说分别剪去了2和3的倍数的四次方,注意这里2和3的公倍数被多减去了,所以要加回来

所以容斥原理也要用

 

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const LL Mod=1000000007 ;
const int M_P=10008 ;
bool isprime[M_P+1] ;
int prime[M_P] ,id;
vector<LL>vec;
void make_prime(){
    memset(isprime,0,sizeof(isprime)) ;
    id=0 ;
    for(int i=2;i<=M_P;i++){
        if(!isprime[i])
            prime[++id]=i ;
        for(int j=1;j<=id&&i*prime[j]<=M_P;j++){
            isprime[i*prime[j]]=1  ;
            if(i%prime[j]==0)
                break ;
        }
    }
}
LL POW(LL x ,LL y){
   LL ans=1 ;
   for(;y;y>>=1){
       if(y&1){
          ans*=x ;
          if(ans>=Mod)
              ans%=Mod ;
       }
       x*=x ;
       if(x>=Mod)
           x%=Mod ;
   }
   return (ans+Mod)%Mod ;
}
LL J ;
LL Sum(LL N){
   LL a=N%Mod ;
   LL b=(N+1)%Mod ;
   LL c=6*N%Mod ;
   LL d=c*N%Mod ;
   LL e=d*N%Mod ;
   LL f=9*N%Mod ;
   LL g=f*N%Mod ;
   LL h=(e+g+N-1)%Mod ;
   LL i=a*b%Mod ;
      i=i*h%Mod ;
      i=i*J%Mod ;
   return (i+Mod)%Mod ;
}
LL gao_coprime_sum(LL N){
    LL ans=0 ;
    int n=vec.size() ;
    for(int i=1;i<(1<<n);i++){
       int now=0 ;
       LL pri=1 ;
       for(int j=0;j<n;j++){
            if(i&(1<<j)){
                now++ ;
                pri*=vec[j] ;
            }
       }
       if(now&1){
           ans=ans+POW(pri,4)*Sum(N/pri) ;
           if(ans>=Mod)
               ans%=Mod ;
       }
       else{
           ans=ans-POW(pri,4)*Sum(N/pri) ;
           if(ans>=Mod)
               ans%=Mod ;
       }
    }
    return ((Sum(N)-ans)%Mod+Mod)%Mod ;
}
int main(){
    make_prime() ;
    J=POW(30,Mod-2) ;
    LL N ,T;
    cin>>T ;
    while(T--){
        cin>>N ;
        vec.clear() ;
        LL M=N ;
        for(int i=1;i<=id&&prime[i]*prime[i]<=M;i++){
            if(M%prime[i]==0){
                   vec.push_back(prime[i]) ;
                   while(M%prime[i]==0)
                       M/=prime[i] ;
            }
            if(M==1)
                break ;
        }
        if(M!=1)
            vec.push_back(M) ;
        cout<<gao_coprime_sum(N)<<endl ;
    }
    return 0 ;
}

 

你可能感兴趣的:(HDU 4059 The Boss on Mars 容斥原理)