HDU 4059 The Boss on Mars 容斥原理+逆元

题意:
求出1~n的与n互质的数的4次方的和
思路:
F(i)表示1~i的4次方的和
Fac[i]表示n的质因子
Ans=F(n)-Fac(i)^4*F(n/Fac(i))+(Fac(i)+Fac(j))^4*F(n/(Fac(i)*Fac(j)))….
奇加偶减
F(i)=n(1 + n)(1 + 2 n)(-1 + 3 n + 3 n^2)/30
因为会边除边取模,所以我们取模的时候乘以30对MOD的逆元然后取模


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
typedef long long LL;
#define maxn 10005
const int mod=1000000007; 
const int inf=(1<<28)-1;
int fac[maxn],len;
void get_fac(LL x)
{
    int t=x;
    len=0;
    for(int i=2;i*i<=t;++i)
    if(t%i==0)
    {
        fac[++len]=i;
        while(t%i==0) t/=i;
    }
    if(t>1) fac[++len]=t;
}
LL ans,n;
LL mul(LL a,LL b)
{
    return (a*b)%mod;
}
LL mul_1(LL a)
{
    return mul(a,mul(a,mul(a,a)));
}
LL mul_2(LL a)
{
    return mul(a,mul(1+a,mul(1+2*a,mul((mul(3,a)+mul(3,mul(a,a))-1+mod)%mod,233333335))));
}
LL mul_3(LL a)
{
    return (mul_1(a)*(mul_2(n/a)))%mod;
}
//pow(1~n,4)=n(1 + n)(1 + 2 n)(-1 + 3 n + 3 n^2)/30
void dfs(int pos,int tot,LL val)
{
    if(val>n) return ;
    if(pos==len+1)
    {
        if(!tot) return ;
        if(tot&1) ans=(ans+mul_3(val))%mod;
        else ans=((ans-mul_3(val))%mod+mod)%mod;
        return ;
    }
    dfs(pos+1,tot+1,val*fac[pos]);
    dfs(pos+1,tot,val);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        get_fac(n);
        ans=0;
        dfs(1,0,1);
        ans=(mul_2(n)-ans+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值