HDU 4059 容斥原理+公式

The Boss on Mars

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

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
题目要求计算对于某一个n,小于n,qieyun互质的所有数的四次方的和,结果模1000000007,。
我们可以先计算出1-n的四次方的和,然后减去与n不互质的部分,也就是说与n含有公因子的部分,我们可以先打素数表,对于每一个n进行质因数分解,
得到所有的质因子,然后用容斥原理把需要去掉的部分去掉,就可以得到答案。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<stdlib.h>
#include<iomanip>
#include<ctype.h>
#include<string.h>
using namespace std;
typedef __int64 ll;
const ll mod=1000000007;
ll isprime[1000100],prime[200000],indexx,cnt,factor[1010];
void getprime()
{
       memset(isprime,1,sizeof(isprime));
       isprime[1]=0;
       ll i,j;
       for(i=2;i<=1000000;i++)
       {
               if(isprime[i])prime[indexx++]=i;
               for(j=0;j<indexx&&i*prime[j]<=1000000;j++)
               {
                      isprime[i*prime[j]]=0;
                      if(i%prime[j]==0)break;
               }
       }
      // cout<<indexx<<endl;
}
void getfactor(ll x)
{
        ll i,j,k;
        cnt=0;
        memset(factor,0,sizeof(factor));
        for(i=0;i<indexx&&prime[i]*prime[i]<=x;i++)
        if(x%prime[i]==0)
        {
                factor[cnt++]=prime[i];
                while(x%prime[i]==0)x/=prime[i];
        }
        if(x>1)factor[cnt++]=x;

}
ll mul(ll a,ll b)
{
        ll ans=0;
        while(b)
        {
                if(b%2==1)ans=(ans+a)%mod;
                b>>=1;
                a=(2*a)%mod;
        }
        return ans;
}
void gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
        if(b==0){d=a;x=1;y=0;}
        else {gcd(b,a%b,d,y,x);y-=x*(a/b);}
}
ll inv(ll a,ll n)
{
        ll d,x,y;
        gcd(a,n,d,x,y);
        return d==1?(x+n)%n:-1;
}
ll sum(ll n)
{
        ll i,j,k;
        i=mul(n,n+1);
        j=inv(30LL,mod);
        i=mul(i,j);
        i=mul(i,2*n+1);
        j=mul(3*n,n);j=(j+3*n-1)%mod;
        i=mul(i,j);
        return i;
}
int main()
{
        getprime();
        ll i,j,k,m,n,T,t,p;
        scanf("%I64d",&T);
        while(T--)
        {
                scanf("%I64d",&n);
                getfactor(n);
               ll ans=sum(n);
              // cout<<cnt<<endl;
               for(i=1;i<(1<<cnt);i++)
               {
                       k=0;
                       m=1;
                      for(j=0;j<cnt;j++)
                      if((i>>j)&1)
                      {
                           m*=factor[j];
                           k++;
                      }
                     // cout<<m<<endl;
                       p=mul(m,m);
                       p=mul(p,p);
                       p=mul(p,sum(n/m));
                      if(k%2==1)ans-=p;
                      else ans+=p;
                      ans=(ans%mod+mod)%mod;
               }
               printf("%I64d\n",ans);
        }
        //while(cin>>n)cout<<sum(n)<<endl;
        return 0;
}

 

posted @ 2013-09-27 22:06  线性无关  阅读(142)  评论(0)    收藏  举报