# [bzoj4407] 于神之怒加强版

## Sample Input

1 2
3 3


## Sample Output

20


## HINT

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

\begin{align} ans&=\sum _{d=1}^{min(n,m)}d^k \sum _{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum _{j=1}^{\lfloor\frac{m}{d}\rfloor} [gcd(i,j)=1]\\ &=\sum _{d=1}^{min(n,m)}d^k \sum _{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum _{j=1}^{\lfloor\frac{m}{d}\rfloor} \sum _{d^\prime|i\&d^\prime|j} \mu(d^\prime)\\ &=\sum _{d=1}^{min(n,m)}d^k \sum _{d^\prime} \mu(d^\prime)\lfloor\frac{n}{dd^\prime}\rfloor \lfloor\frac{m}{dd^\prime}\rfloor \\ &=\sum _{d=1}^{min(n,m)}d^k \sum _{d|T} \mu(\frac{T}{d})\lfloor\frac{n}{T}\rfloor \lfloor\frac{m}{T}\rfloor \\ &=\sum _{T} \lfloor\frac{n}{T}\rfloor \lfloor\frac{m}{T}\rfloor \sum _{d|T}d^k \mu(\frac{T}{d})\\ \end {align}

$f$为：

$f(n)=\sum_{d|n}d^k\mu(\frac{n}{d})$

$f(p^a)=\sum _{i=0}^{a}(p^i)^k\mu(p^{a-i})=p^{ak}-p^{(a-1)k}$

#include<bits/stdc++.h>
using namespace std;

#define int long long

x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

const int maxn = 5e6+1;
const int mod = 1e9+7;

int pri[maxn],vis[maxn],mu[maxn],f[maxn],tot,n,m,k;

int qpow(int a,int x) {
int res=1;
for(;x;x>>=1,a=a*a%mod) if(x&1) res=res*a%mod;
return res;
}

void sieve() {
mu[1]=1;
for(int i=2;i<=n;i++) {
if(!vis[i]) pri[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&i*pri[j]<=n;j++) {
vis[i*pri[j]]=1;
if(!(i%pri[j])) {mu[i*pri[j]]=0;break;}
mu[i*pri[j]]=-mu[i];
}
}
for(int d=1;d<=n;d++) {
int res=qpow(d,k);
for(int i=1;i*d<=n;i++) (f[i*d]+=res*mu[i])%=mod;
}
for(int i=1;i<=n;i++) f[i]=(f[i]+f[i-1])%mod;
}

signed main() {
while(t--) {
int T=1,ans=0;
while(T<=n&&T<=m) {
int pre=T;T=min(n/(n/T),m/(m/T));
ans=(ans+(n/T)*(m/T)%mod*(f[T]-f[pre-1])%mod)%mod;
T++;
}
write((ans%mod+mod)%mod);
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long

x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

const int maxn = 5e6+1;
const int mod = 1e9+7;

int pri[maxn],vis[maxn],mu[maxn],f[maxn],tot,n,m,k,p[maxn];

int qpow(int a,int x) {
int res=1;
for(;x;x>>=1,a=a*a%mod) if(x&1) res=res*a%mod;
return res;
}

void sieve() {
f[1]=1;
for(int i=2;i<=n;i++) {
if(!vis[i]) pri[++tot]=i,p[tot]=qpow(i,k),f[i]=p[tot]-1;
for(int j=1;j<=tot&&i*pri[j]<=n;j++) {
vis[i*pri[j]]=1;
if(!(i%pri[j])) {f[i*pri[j]]=f[i]*p[j]%mod;break;}
f[i*pri[j]]=f[i]*(p[j]-1)%mod;
}
}for(int i=1;i<=n;i++) f[i]=(f[i]+f[i-1])%mod;
}

signed main() {
//int PRE=clock();
sieve();
//cerr << (double) (clock()-PRE)/CLOCKS_PER_SEC << endl;
while(t--) {
int T=1,ans=0;
while(T<=n&&T<=m) {
int pre=T;T=min(n/(n/T),m/(m/T));
ans=(ans+(n/T)*(m/T)%mod*(f[T]-f[pre-1])%mod)%mod;
T++;
}
write((ans%mod+mod)%mod);
}
return 0;
}



posted @ 2018-12-02 18:22  Hyscere  阅读(205)  评论(0编辑  收藏  举报