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): 1934    Accepted Submission(s): 580


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
 

題解及代碼:

       這道題目的綜合性還是很強的。首先說一下題目,就是求小於n並且與n互素的數的四次方的和。

       說一下思路吧:首先我們求出1---n-1的所有的數的四次方的和,之後將n進行素因子分解,求出n的所有因子,然後減去包含這些因子的數的四次方就可以了。

       大體上的思路有了,來處理一下細節:1.首先我們要求出四次方和的公式   2.素數打表   3.求逆元(因爲四次方和公式有一個分母,取餘時要乘上逆元)

       4.素因子分解    5.容斥原理

       搞定這5步,我們這道題就能做了,所以說綜合性非常強。

具體見代碼吧:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const long long mod=1000000007,q=233333335;//p爲逆元,用費馬小定理求出
bool prime[10010];
int  p[1400];
int  k=0;

//四次方和計算公式
long long cal(long long n)
{
    if(n==0) return 0;
    return (n*(n+1)%mod*(2*n+1)%mod)%mod*((3*n*n+3*n-1)%mod*q%mod)%mod;
}

//容斥原理
void dfs(int base,int num_p,long long n,long long m,long long nt,long long mu,long long &sum,long long tab_p[])
{
    if(nt==m)
    {
        long long b=n/mu;
        if(m%2==0)
        {
            sum=(sum-mu*mu%mod*mu%mod*mu%mod*cal(b)%mod+mod)%mod;
        }
        else
        {
            sum=(sum+mu*mu%mod*mu%mod*mu%mod*cal(b)%mod)%mod;
        }
        return;
    }
    for(long long i=base; i<num_p; i++)
    {
        dfs(i+1,num_p,n,m,nt+1,mu*tab_p[i],sum,tab_p);
    }
}

//素數打表
void isprime()
{
    long long i,j;
    memset(prime,true,sizeof(prime));
    prime[0]=prime[1]=false;
    for(i=2; i<10010; i++)
    {
        if(prime[i])
        {
            p[k++]=i;
            for(j=i*i; j<10010; j+=i)
                prime[j]=false;
        }
    }
}

int main()
{
    isprime();
    long long n,ans,tab_p[1400];
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%I64d",&n);
        n=n-1;
        ans=cal(n);
        long long m=n,t=n+1;
        int num_p=0;
        for(int i=0; i<k&&p[i]*p[i]<=t; i++) //素因子分解
            if(t%p[i]==0)
            {
                tab_p[num_p++]=p[i];
                while(t%p[i]==0)
                {
                    t/=p[i];
                }
            }
        if(t>1)  tab_p[num_p++]=t;

        /*//輸出測試
        for(int i=0;i<num_p;i++)
        {
            printf("%d ",tab_p[i]);
        }
        puts("");
        //測試結束
        */

        long long sum=0;
        for(int i=0; i<num_p; i++)  //將不互素的部分減去
        {
            n=m/tab_p[i];
            sum=(sum+tab_p[i]*tab_p[i]%mod*tab_p[i]%mod*tab_p[i]%mod*cal(n))%mod;
        }

        for(long long i=2; i<=num_p; i++)  //容斥部分求解
        dfs(0,num_p,m,i,0LL,1LL,sum,tab_p);

        printf("%I64d\n",(ans-sum+mod)%mod);
    }
    return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章