# BZOJ 4407: 于神之怒加强版

## 4407: 于神之怒加强版

Time Limit: 80 Sec  Memory Limit: 512 MB
Submit: 560  Solved: 271
[Submit][Status][Discuss]

1 2
3 3

20

## HINT

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

## 分析：

$\sum _{i=1}^{n}\sum _{i=1}^{m} gcd(i,j)^k$

$=\sum _{d=1}^{n} d^k \sum _{x=1}^{n} \mu (x) \left \lfloor \frac{n}{xd} \right \rfloor \left \lfloor \frac{m}{xd} \right \rfloor$

$\sum _{d=1}^{n} d^k \sum _{d\mid y} \mu (\frac{y}{d}) \left \lfloor \frac{n}{y} \right \rfloor \left \lfloor \frac{m}{y} \right \rfloor$

$=\sum _{y=1}^{n} \left \lfloor \frac{n}{y} \right \rfloor \left \lfloor \frac{m}{y} \right \rfloor \sum _{d\mid y} \mu( \frac{y}{d} ) d^k$

$f(n)=\Pi _{i=1}^{t} f(p_i^{x_i})$

$=\Pi _{i=1}^{t} \sum _{d\mid p_i^{x_i}} \mu (\frac{p_i^{x_i}}{d}) d^k$

$=\Pi _{i=1}^{t} \mu (1) p_i^{x_i} +\mu (p_i) p_i^{k(x_i-1)}$

$=\Pi _{i=1}^{t} p_i^{kx_i}-p_i^{k(x_i-1)}$

$=\Pi _{i=1}^{t} p_i^{k(x_i-1)}(p_i^k-1)$

$f[x*p]=f[x]f[p]$   ---   $x$%$p\neq 0$

$f[x*p]=f[x]*p^k$ ---   $x$%$p = 0$

## 代码：

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;

const int maxn=5000000+5,mod=1e9+7;

int n,m,k,ans,cas,cnt,f[maxn],g[maxn],pri[maxn],vis[maxn];

inline int power(int x,int y){
int res=1;
while(y){
if(y&1)
res=1LL*res*x%mod;
x=1LL*x*x%mod,y>>=1;
}
return res;
}

signed main(void){
scanf("%d%d",&cas,&k);g[1]=1;
for(int i=2;i<=5000000;i++){
if(!vis[i])
pri[++cnt]=i,vis[i]=1,f[i]=power(i,k),g[i]=f[i]-1;
for(int j=1;j<=cnt&&1LL*i*pri[j]<=5000000;j++){
vis[i*pri[j]]=1;
if(i%pri[j]==0){
g[i*pri[j]]=1LL*g[i]*f[pri[j]]%mod;
break;
}
g[i*pri[j]]=1LL*g[i]*g[pri[j]]%mod;
}
}
for(int i=2;i<=5000000;i++)
g[i]=(g[i-1]+g[i])%mod;
while(cas--){
scanf("%d%d",&n,&m);ans=0;
if(n>m) swap(n,m);
for(int i=1,r;i<=n;i=r+1){
r=min(n/(n/i),m/(m/i));
ans=(ans+1LL*(n/i)*(m/i)%mod*((g[r]-g[i-1]+mod)%mod)%mod)%mod;
}
printf("%d\n",ans);
}
return 0;
}

By NeighThorn

posted @ 2017-04-07 20:10  NeighThorn  阅读(407)  评论(2编辑  收藏  举报