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

2011 Asia Dalian Regional Contest

题解及代码:

这道题目的综合性还是很强的。首先说一下题目,就是求小于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;
}
时间: 2024-10-16 12:15:51

hdu 4059 The Boss on Mars的相关文章

数论 + 容斥 - HDU 4059 The Boss on Mars

The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若自己手动推公式的话,还是需要一定的数学基础. 总的思路:先求出sum1=(1^4)+(2^4)+...(n^4),再求出sum2=(1~n中与n不互质的数的四次方的和),answer=sum1-sum2. 如何求sum1呢? 有两种方法: 1.数列差分.由于A={Sn}={a1^4+a2^4+...an^4}

HDU 4059 The Boss on Mars(数论)

题目大意:给你一个n(10^8)以内,让你求出1-n中与n互质的数x^4的和. 解题思路:先把n进行分解质因数,然后容斥求出所有与n不互质的数x^4的和,然后做减法用总的减去不互质的就是互质的. 注意:1^4+2^4+--+n^4 = n(n+1)(2n+1)(3n^2+3n-1)/30. The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot

hdu 4059 The Boss on Mars(容斥)

http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4,现在减去与n互质的数的4次方,问共减少了多少. 容斥原理,可以先把与n不互质的数的4次方求出来.那就先对n进行质因子分解,对质因子的组合运用容斥原理,质因子个数为奇数就加,偶数就减.其实与求[1,n]内与n互质的数的个数类似,该题重点是计算,防止乘法溢出. 对于求解1^4 + 2^4 + 3^4+.....+n^4,可以先类比1^2+2^2

hdu 4059 The Boss on Mars(纳入和排除)

http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多少. 容斥原理.能够先把与n不互质的数的4次方求出来.那就先对n进行质因子分解,对质因子的组合运用容斥原理.质因子个数为奇数就加,偶数就减.事实上与求[1,n]内与n互质的数的个数类似,该题重点是计算,防止乘法溢出. 对于求解1^4 + 2^4 + 3^4+.....+n^4,能够先类比1^2+2^

HDU 4059 The Boss on Mars ( 容斥原理)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题意: 给定一个数n求小于n的与n互斥的数的四次方的和. 分析: 我们可以求出从1~n的所有数的四次方的和sum1,然后容斥求出1~n所有与n不互斥的数的四次方的和sum2: ans =sum1 - sum2; 设f(n)表示从1~n的所有数的四次方的和 f(n)=1/30*n*(n+1)(2n+1)(3n^2+3n-1); 推倒如下: (n+1)^5-n^5=5n^4+10n^3+10n^

HDU 4059 The Boss on Mars-矩阵+容斥

错了29遍,终成正果..... 根据题意,很容易的可以想到容斥. 然后的问题就是如何求 sum(n)=1^4+2^4+3^4+....+n^4; 有三种道路: 很显然:1^4+2^4+3^4+....+n^4=(n^5)/5+(n^4)/2+(n^3)/3-n/30: 则1,用java的大数去敲这个的代码. 2,用c++敲,但是用到分数取模,求逆元. 3,用c++敲,但是不用这个公式,用矩阵去构造sum(n). 我用的是第三种.但是第三种有的缺陷,就是时间复杂度有点高. 接下来的问题就是如何优化

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): 2327    Accepted Submission(s): 718 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), and

HDU 1800 Flying to the Mars (水题)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11099    Accepted Submission(s): 3572 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 4059 容斥初步练习

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #define LL long long 6 using namespace std; 7 const LL Mod=1000000007; 8 const LL Maxn=60010; 9 LL Factor[35],cnt,n,m,tot,Rev,Kase,Prime[Maxn];