hdu4059 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): 1335    Accepted Submission(s): 401

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
 
Recommend
lcy
这题,我们比赛都写的差不多了,就是在算的时候由于n的平方,爆掉了,不知道哪里错了,一直卡到了最后!也没发现,唉,可惜了!
首先我们可以得到公式1^4+2^4+3^4+.....+m^4为m*(1+m)*(1+2*m)(3*m*m+3*m-1);然后,用个容斥定理就解决问题了!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define mod 1000000007
__int64 vec[33];
int ans;
__int64 prime[10500],pans;
__int64 re;
__int64 n;
int init()
{
    int i;
    memset(prime,0,sizeof(prime));
    for(i=2;i<=10000;i++)
    {
        if(!prime[i])
        for(int j=i+i;j<10050;j=j+i)
        {
            prime[j]=1;
        }
    }
    pans=0;
    for(i=2;i<=10000;i++)
    {
        if(!prime[i])
        prime[pans++]=i;
    }
    return 1;
}
__int64 ff(__int64 k)
{
    int i,j;
    __int64 m=(__int64)(n/k),a[5],b[]={2,3,5};
    a[0]=m;a[3]=(1+m),a[1]=(1+2*m),a[2]=(3*m*m+3*m-1);
     for(j=0;j<=2;j++)
    {
        for(i=0;i<4;i++)
        {
            if(a[i]%b[j]==0)
            {
                a[i]/=b[j];
                break;
            }
        }
    }
    return  a[2]%mod*a[0]%mod*a[1]%mod*a[3]%mod*k%mod*k%mod*k%mod*k%mod;
}
void dfs(int now,int len,__int64 s )
{
    int i;
    if(len>=ans)
    return;
    if(now>=ans)
    return;
    if(len&1)
    {
       re+=ff(s);
       re%=mod;
       if(re<0)
       re+=mod;
    }
    else
    {
       re-=ff(s);
        re%=mod;
       if(re<0)
       re+=mod;
    }
    for(i=now+1;i<ans;i++)
    {
        dfs(i,len+1,s*vec[i]);
    }
}
bool isp(__int64 ii,__int64 pri)
{
    if(pri==1)
    return false;
    __int64 m=sqrt((double)pri);
    for(__int64 i=ii+1;i<pans&&prime[i]<=m;i++)
    {
        if(pri%prime[i]==0)
        return false;
    }
    return true;
}
int main()
{
    init();
    int tcase,i;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%I64d",&n);
        ans=0;
        __int64 m=(__int64)sqrt((double)n)+1;
        __int64 tempn=n;
        bool flag=true;
        if(isp(-1,n))
        flag=false;
        else
        flag=true;__int64 ii=prime[0];
        for(__int64 i1=0;flag&&ii<=tempn&&i1<pans;i1++)
        {
            ii=prime[i1];
           if(tempn%ii==0)
           {
              vec[ans++]=ii;
              while(tempn%ii==0)
              {
                  tempn/=ii;
              }
              if(isp(i1,tempn))
              flag=false;
            }
        }
        if(!flag)
        vec[ans++]=tempn;
        re=ff(1);
        for(i=0;i<ans;i++)
        dfs(i,0,vec[i]);
        printf("%I64d
",re);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/pangblog/p/3299336.html