HDU 4059 容斥初步练习

时间:2024-01-05 13:58:25
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
const LL Mod=;
const LL Maxn=;
LL Factor[],cnt,n,m,tot,Rev,Kase,Prime[Maxn];
bool vis[Maxn]; inline LL Quick_Pow(LL x,LL y)
{
LL Ret=;
while (true)
{
if (y&) Ret=(Ret*x)%Mod;
x=(x*x)%Mod; y>>=;
if (y==) break;
}
return Ret;
} inline void Make_Prime()
{
memset(vis,false,sizeof(vis));
for (LL i=;i<Maxn;i++)
{
if (!vis[i]) Prime[++tot]=i;
for (LL j=;j<=tot && Prime[j]*i<Maxn;j++)
{
vis[Prime[j]*i]=true;
if (i%Prime[j]==) break;
}
}
} inline LL Calc(LL N)
{
LL Ret=N;
Ret=(Ret*(N+))%Mod;
Ret=(Ret*(*N+))%Mod;
Ret=(Ret*((*N*N+*N-)%Mod))%Mod;
Ret=(Ret*Rev)%Mod;
return Ret;
}
inline void Get_Factor(LL P)
{
cnt=;
for (LL i=;i<=tot && Prime[i]<=P;i++)
if (P%Prime[i]==)
{
Factor[++cnt]=Prime[i];
while (P%Prime[i]==) P/=Prime[i];
}
if (P!=) Factor[++cnt]=P;
}
inline LL Pow2(LL x) {return (x*x)%Mod;}
inline LL Pow4(LL x) {return (Pow2(x)*Pow2(x))%Mod;}
LL Dfs(LL d,LL start)
{
LL Ret=;
for (LL i=start;i<=cnt;i++)
{
LL tmp=Pow4(Factor[i]);
Ret=(Ret+(tmp*Calc(d/Factor[i]))%Mod)%Mod;
Ret=(Ret-(tmp*Dfs(d/Factor[i],i+))%Mod+Mod)%Mod;
}
return Ret;
}
inline LL Solve()
{
Get_Factor(n);
return ((Calc(n)%Mod)-(Dfs(n,))%Mod+Mod)%Mod;
}
int main()
{
scanf("%lld",&Kase);
Rev=Quick_Pow(,Mod-);
Make_Prime(); for (LL kase=;kase<=Kase;kase++)
{
scanf("%lld",&n);
printf("%lld\n",Solve());
}
return ;
}

HDU 4059

求的是与n互质的数的四次方的和。 首先四次方有个公式。把1~n的然后减去n的约数的四次方即可,这就需要用到容斥了。

Sum(2)-(Sum(2*3)+Sum(2*5)+Sum(2*7)..)+(Sum(2*3*5)+Sum(2*3*7)+Sum(3*5*7)..)-..