P4449 于神之怒加强版
推式子
∑ i = 1 n ∑ j = 1 n g c d ( i , j ) h \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} gcd(i, j) ^ h i=1∑nj=1∑ngcd(i,j)h
= ∑ d = 1 n d h ∑ i = 1 n d ∑ j = 1 m d g c d ( i , j ) = = 1 =\sum_{d = 1} ^{n} d ^ h \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}}gcd(i, j) == 1 =d=1∑ndhi=1∑dnj=1∑dmgcd(i,j)==1
= ∑ d = 1 n d h ∑ i = 1 n d ∑ j = 1 m d ∑ k ∣ g c ( i , j ) μ ( k ) =\sum_{d = 1} ^{n} d ^ h \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}}\sum_{k \mid gc(i, j)}\mu(k) =d=1∑ndhi=1∑dnj=1∑dmk∣gc(i,j)∑μ(k)
= ∑ d = 1 n d h ∑ k = 1 n d μ ( k ) ∑ i = 1 n d k ∑ j = 1 m d k =\sum_{d = 1} ^{n} d ^ h \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \sum_{i = 1} ^{\frac{n}{dk}} \sum_{j = 1} ^{\frac{m}{dk}} =d=1∑ndhk=1∑dnμ(k)i=1∑dknj=1∑dkm
= ∑ d = 1 n d h ∑ k = 1 n d μ ( k ) ⌊ n k d ⌋ ⌊ m k d ⌋ =\sum_{d = 1} ^{n} d ^ h \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \lfloor\frac{n}{kd}\rfloor \lfloor\frac{m}{kd}\rfloor =d=1∑ndhk=1∑dnμ(k)⌊kdn⌋⌊kdm⌋
t = k d t = kd t=kd
= ∑ t = 1 n ⌊ n t ⌋ ⌊ m t ⌋ ∑ d ∣ t d h μ ( t d ) =\sum_{t = 1} ^{n} \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor \sum_{d \mid t} d ^ h \mu(\frac{t}{d}) =t=1∑n⌊tn⌋⌊tm⌋d∣t∑dhμ(dt)
所以我们只要再预处理出 f ( t ) = ∑ d ∣ t d h μ ( t d ) f(t) = \sum_{d \mid t} d ^ h \mu(\frac{t}{d}) f(t)=∑d∣tdhμ(dt)即可,由于后面是一个积性函数我们可以直接通过素数筛来得到。
代码
/*
Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N = 5e6 + 10, mod = 1e9 + 7;
bool st[N];
ll mu[N], n, m, k, sum[N], f[N];
int prime[N], tot;
ll quick_pow(ll a, ll n, ll mod) {
ll ans = 1;
while(n) {
if(n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
return ans;
}
void mobius() {
sum[1] = 1;
for (int i = 2; i < N; i++) {
if(!st[i]){
prime[++tot] = i;
f[tot] = quick_pow(i, k, mod);
sum[i] = (f[tot] + mod - 1) % mod;
}
for(int j = 1; j <= tot && i * prime[j] < N; j++) {
st[i * prime[j]] = 1;
if(i % prime[j] == 0) {
sum[i * prime[j]] = sum[i] * f[j] % mod;
break;
}
sum[i * prime[j]] = sum[i] * sum[prime[j]] % mod;
}
}
for(int i = 2; i < N; i++) {
sum[i] = (sum[i - 1] + sum[i]) % mod;
}
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T = read();
k = read();
mobius();
while(T--) {
n = read(), m = read();
if(n > m) swap(n, m);
ll ans = 0;
for(ll l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans = (ans + (n / l) * (m / l) % mod * (sum[r] - sum[l - 1] + mod) % mod + mod) % mod;
}
printf("%lld\n", ans);
}
return 0;
}