# 【BZOJ-4407】于神之怒加强版 莫比乌斯反演 + 线性筛

## 4407: 于神之怒加强版

Time Limit: 80 Sec  Memory Limit: 512 MB
Submit: 241  Solved: 119
[Submit][Status][Discuss]

1 2
3 3

20

## HINT

1<=N,M,K<=5000000,1<=T<=2000

## Solution

$$\sum_{d=1}^{n}d^{k}\sum_{i=1}^{n}\sum_{j=1}^{m}\left \lfloor gcd\left ( i,j \right )= d \right \rfloor$$

$$f\left ( d \right )= \sum_{x=1}^{\left \lfloor \frac{n}{d} \right \rfloor}\mu \left ( x \right )\left \lfloor \frac{n}{dx} \right \rfloor\left \lfloor \frac{m}{dx} \right \rfloor$$

$$Ans= \sum_{d=1}^{n}d^{k}\times f(d)$$

$$Ans= \sum_{y}^{n}\left \lfloor \frac{n}{y} \right \rfloor\left \lfloor \frac{m}{y} \right \rfloor\sum_{d|y}d^{k}\mu \left ( \frac{y}{d} \right )$$

## Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define maxn 5000010
#define p 1000000007
int T,K,N,M;
long long quick_pow(long long x,int y)
{
long long re=1; x=x%p; y%=p;
for (int i=y; i; i>>=1,x=x*x%p)
if (i&1) re=re*x%p;
return re;
}
bool flag[maxn];long long F[maxn],prime[maxn],cnt,sum[maxn];
void prework()
{
flag[1]=1; F[1]=1; sum[1]=1;
for (int i=2; i<maxn; i++)
{
if (!flag[i]) prime[++cnt]=i,F[i]=quick_pow(i,K)-1;
for (int j=1; j<=cnt && i*prime[j]<maxn; j++)
{
flag[i*prime[j]]=1;
if (!(i%prime[j]))
{F[i*prime[j]]=F[i]*quick_pow(prime[j],K)%p;break;}
else F[i*prime[j]]=F[i]*F[prime[j]]%p;
}
sum[i]=sum[i-1]+F[i]%p;
}
}
void work(int n,int m)
{
if (n>m) swap(n,m);
long long ans=0;
for (int j,i=1; i<=n; i=j+1)
j=min(m/(m/i),n/(n/i)),
ans+=(sum[j]-sum[i-1]+p)%p*(n/i)%p*(m/i)%p,ans%=p;
printf("%lld\n",ans);
}
int main()
{
}