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

78 篇文章 0 订阅
56 篇文章 0 订阅


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
  
  
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
 
题目链接: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 long
int 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值