码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4059 The Boss on Mars

时间:2014-10-16 20:35:53      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   

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;
}






hdu 4059 The Boss on Mars

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://blog.csdn.net/knight_kaka/article/details/40151921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!