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

占个坑~~~ 晚上回去补

#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;
}

你可能感兴趣的:(HDU 4059 The Boss on Mars 容斥原理+逆元)