(hdu4059)The Boss on Mars(费马小定理+快速幂)

本文介绍了一个有趣的问题:如何计算火星上一家公司在一年内因解雇与员工数量互质的员工而节省下来的薪资总额。通过数学分析和编程实现,提供了一种有效的方法来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2797 Accepted Submission(s): 894

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

题意:求1-n中与n互质的数的4次方之和,即S=a1^4+a2^4+……; a1,a2……均小于等于n且与n互质。

分析:(1^4+2^4+……+n^4)=(n*(n+1)(2n+1)(3*n*n+3*n-1))/30;
1.因为要求和,有除法取模,所以转化为逆元,求解逆元,用快速幂+费马小定理
2.因为要求[1,n]内与n互质的数,转化为求不互质的数的个数,先分解n,找出所有的素因子,然后利用容斥定理

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
LL n,ans;
LL pow_mod(LL a,LL b)
{
    LL ans=1;
    a%=b;
    while(b>0)
    {
        if(b&1) ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
LL fermat(LL a,LL p)///费马小定理求逆元
{
    return pow_mod(a,p-2);
}
LL pow4(LL n)
{
    return n*n%mod*n%mod*n%mod;
}
LL cal(LL n)///计算(S/30)%mod,转化为逆元
{
    return n*(n+1)%mod*(2*n+1)%mod*(3*n*n%mod+3*n-1)%mod*fermat(30,mod)%mod;
}
LL solve()
{
    vector<LL>vec;
    LL tmp=n;
    for(LL i=2; i*i<=tmp; i++)
    {
        if(tmp%i==0)
        {
            vec.push_back(i);
            while(tmp%i==0)
                tmp/=i;
        }
    }
    if(tmp>1) vec.push_back(tmp);
    LL cnt=vec.size();
    for(LL i=1; i<(1<<cnt); i++)///二进制位实现容斥定理
    {
        LL mul=1,num=0;
        for(LL j=0; j<cnt; j++)
        {
            if(i&(1<<j))///  位与!!!
            {
                num++;
                mul*=vec[j];
            }
        }
        LL t=pow4(mul)*cal(n/mul);///cal(n/mul)是计算一共有多少不互质的数的个数的4次方,再取模
        if(num&1) ans-=t;///减去不互质的数的4次方,
        else ans+=t;///加上重复减去了的数的4次方
        ans=(ans%mod+mod)%mod;///保证 ans为正
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        ans=cal(n);
        printf("%lld\n",solve());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值