zoj The Boss on Mars 求sigma(i^4) i与n互质 容斥原理

一家位于火星的大公司ACM面临着特殊的薪资发放挑战。由于火星周围没有卫星,员工们只能按年领取薪水,且数额为工作编号的四次方。本文介绍了一种算法,用于计算在解雇与总员工数互质的员工后,老板可以节省多少火星币。

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

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


//

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define bignum long long
const bignum mod=1000000007;
bignum exgcd(bignum a,bignum b,bignum& x,bignum& y)
{
    if(b==0) return x=1,y=0,a;
    bignum r=exgcd(b,a%b,x,y);
    bignum t=x;
    x=y;
    y=t-(a/b)*y;
    return r;
}
bignum calc(bignum a,bignum b,bignum mod)
{
    bignum x,y;
    bignum r=exgcd(b,mod,x,y);
    x*=a;
    x=(x%mod+mod)%mod;
    return x;
}
bignum inv2,inv3,inv5;
void init()
{
    inv2=calc(1,2,mod);
    inv3=calc(1,3,mod);
    inv5=calc(1,5,mod);
}
bignum _pow(bignum n,int p)
{
    bignum cnt=1;
    for(int i=0;i<p;i++)
    {
        cnt=(cnt*n)%mod;
    }
    return cnt;
}
bignum sigma_4(bignum n)
{
    bignum cnt=_pow(n+1,5)-1;
    bignum tmp=5*n%mod*n%mod*(n+1)%mod*(n+1)%mod*inv2%mod;
    tmp+=5*n%mod*(n+1)%mod*(n+n+1)%mod*inv3%mod;
    tmp%=mod;
    tmp+=5*n%mod*(n+1)%mod*inv2%mod;
    tmp%=mod;
    tmp+=n;
    tmp%=mod;
    cnt=((cnt-tmp)%mod+mod)%mod;
    return cnt*inv5%mod;
}
const int maxn=11000;
int flag[maxn],prime[maxn],pl;
void _prime()
{
    for(int i=2;i<maxn;i++)
    {
        if(flag[i]==0) prime[pl++]=i;
        for(int j=0;j<pl&&i*prime[j]<maxn;j++)
        {
            flag[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int fac[maxn],facn;
void _fac(int n)
{
    facn=0;
    for(int i=0;i<pl&&prime[i]*prime[i]<=n;i++)
    {
        if(n%prime[i]==0)
        {
            fac[facn++]=prime[i];
            while(n%prime[i]==0) n/=prime[i];
        }
    }
    if(n>1) fac[facn++]=n;
}
int main()
{
    init();
    _prime();
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        int n;scanf("%d",&n);
        _fac(n);
        int ans=sigma_4((bignum)n);
        for(int i=1;i<(1<<facn);i++)
        {
            int flag=0;
            int cnt=1;
            for(int j=0;j<facn;j++)
            {
                if(i&(1<<j))
                {
                    flag++;cnt*=fac[j];
                }
            }
            int tmp=sigma_4((bignum)n/cnt)*_pow(cnt,4)%mod;
            if(flag&1) ans-=tmp;
            else ans+=tmp;
            ans=(ans%mod+mod)%mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值