# BZOJ4407: 于神之怒加强版

Time Limit: 80 Sec Memory Limit: 512 MB
Submit: 1220 Solved: 550
[Submit][Status][Discuss]

1 2

3 3

20

## HINT

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

# 题解

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

$=\sum_{d=1}^{min(n,m)}\sum_{i=1}^{n}\sum_{j=1}^{m}d^k\times[gcd(i,j)=d]$

$=\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_{t|gcd(i,j)}\mu(t)$

$=\sum_{d=1}^{min(n,m)}d^k\sum_{t=1}^{min(\lfloor \frac{n}{d} \rfloor,\lfloor \frac{m}{d} \rfloor)}\mu(t)\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{m}{d} \rfloor}\sum_{t|i}\sum_{t|j}1$

$=\sum_{d=1}^{min(n,m)}d^k\sum_{t=1}^{min(\lfloor \frac{n}{d} \rfloor,\lfloor \frac{m}{d} \rfloor)}\mu(t)\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{t|i}1\sum_{j=1}^{\lfloor \frac{m}{d} \rfloor}\sum_{t|j}1$

$=\sum_{d=1}^{min(n,m)}d^k\sum_{t=1}^{min(\lfloor \frac{n}{d} \rfloor,\lfloor \frac{m}{d} \rfloor)}\mu(t)\lfloor\frac{n}{dt}\rfloor\lfloor\frac{m}{dt}\rfloor$

$T=dt$，有

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

$=\sum_{T=1}^{min(n,m)}\lfloor \frac{n}{T} \rfloor \lfloor\frac{m}{T}\rfloor\sum_{d|T}\mu(\frac{T}{d})d^k$

$\sum_{d|T}\mu(\frac{T}{d})d^k$这东西其实就是$\mu$$g$的卷积

$f(T)=(\mu*g)(T)=\sum_{d|T}\mu(\frac{T}{d})d^k$

$f(\frac{T}{a})=f(a^{c-1})f(q)=f(q)(a^{k(c-1)}-a^{k(c-2)})$

$f(T)=f(a^c)f(q)=f(q)(a^{kc}-a^{k(c-1)})$

$f(T)=f(\frac{T}{a})a^k$

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
inline long long max(long long a, long long b){return a > b ? a : b;}
inline long long min(long long a, long long b){return a < b ? a : b;}
inline void swap(long long &x, long long &y){long long  tmp = x;x = y;y = tmp;}
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long MAXN = 5000000;
const long long MOD = 1e9 + 7;

long long pow(long long a, long long b)
{
long long r = 1, base = a;
for(;b;b >>= 1)
{
if(b & 1) r *= base, r %= MOD;
base *= base, base %= MOD;
}
return r;
}

long long t, k, p[MAXN + 10], bp[MAXN + 10], tot, f[MAXN], n, m;

void make_f()
{
f[1] = 1;
for(long long i = 2;i <= MAXN;++ i)
{
if(!bp[i]) p[++ tot] = i, f[i] = (pow(i, k) - 1 + MOD) % MOD;
for(long long j = 1;j <= tot && p[j] * i <= MAXN;++ j)
{
bp[i * p[j]] = 1;
if(i % p[j] == 0)
{
f[i * p[j]] = f[i] * pow(p[j], k) % MOD;
break;
}
f[i * p[j]] = f[i] * f[p[j]] % MOD;
}
}
for(long long i = 1;i <= MAXN;++ i) f[i] += f[i - 1];
}

int main()
{
make_f();
for(;t;-- t)
{
long long ans = 0, r, mi = min(n, m);
for(long long T = 1;T <= mi;++ T)
{
r = min(n/(n/T), min(m/(m/T), mi));
ans += ((n/T) * (m/T)) % MOD * (f[r] - f[T - 1]) % MOD;
if(ans >= MOD) ans -= MOD;
T = r;
}
printf("%lld\n", ans);
}
return 0;
}

posted @ 2018-03-23 08:13  嘒彼小星  阅读(209)  评论(0编辑  收藏  举报