HDU 4059 The Boss on Mars (容斥+快速幂+分解质因数)

来源:互联网 发布:yum 安装snmp 编辑:程序博客网 时间:2024/05/16 07:24


The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2304    Accepted Submission(s): 701


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
245
 
Sample Output
82354
Hint
Case1: sum=1+3*3*3*3=82Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
 
Author
ZHANG, Chao
 
Source
2011 Asia Dalian Regional Contest
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4059

题目大意:求1-n中所有与n互质的数的四次方的和

题目分析:开始准备莫比乌斯反演,一看1e8,显然不行,只能分解质因数用DFS容斥了,先求出n^4的和:k*(k+1)*(2*k+1)*(3*k^2+3*k-1)/30,推导过程不写了,然后减去与n不互质的即可

#include <cstdio>#include <cstring>#define ll long longint const MOD = 1e9 + 7;int fac[10000], cnt;ll n, ans;ll qpow(ll x, ll n){    ll res = 1;    while(n)    {        if(n & 1)            res = (res * x) % MOD;        x = (x * x) % MOD;        n >>= 1;    }    return res;}ll cal(ll x) //1 + 2^4 + 3^4 + .. + x^4{    ll t1 = (x * (x + 1) % MOD) * ((2 * x + 1) % MOD);    ll t2 = ((3 * x * x + 3 * x - 1) % MOD) * qpow(30ll, MOD - 2);    return ((t1 % MOD) * (t2 % MOD)) % MOD;}void DFS(int pos, ll pre, int sgin){    if(pos >= cnt || pre > n)        return;    ll t = 1, cur = pre * fac[pos];    for(int i = 0; i < 4; i++)        t = (t * cur) % MOD;    t = (t * cal(n / cur)) % MOD;   //所有cur的倍数的四次方和    ans = ((ans % MOD) + (sgin * t) % MOD + MOD) % MOD; //容斥    if(n % cur == 0)        DFS(pos + 1, cur, -sgin);      DFS(pos + 1, pre, sgin);}int main(){    int T;    scanf("%d", &T);    while(T--)    {        memset(fac, 0, sizeof(fac));        cnt = 0;        scanf("%I64d", &n);        ll nn = n;        for(int i = 2; i * i <= nn; i++)        {            if(nn % i == 0)            {                fac[cnt ++] = i;                while(nn % i == 0)                    nn /= i;            }        }        if(nn != 1)            fac[cnt ++] = nn;        ans = cal(n);        DFS(0, 1, -1);        printf("%I64d\n", ans);    }   }


0 0
原创粉丝点击