# bzoj4407 于神之怒加强版

【题解】

# include <stdio.h>
# include <string.h>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, m, K;

const int F = 5000000;
bool isnp[F + 10];
int p[F/3], pn=0;
int f[F + 10], s[F/3];

inline int pwr(int a, int b) {
int ret = 1;
while(b) {
if(b&1) ret = 1ll * ret * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return ret;
}

inline void sieve() {
f[1] = 1;
for (int i=2; i<=F; ++i) {
if(!isnp[i]) {
p[++pn] = i;
s[pn] = pwr(i, K);
f[i] = s[pn] - 1;
if(f[i] < 0) f[i] += mod;
}
for (int j=1; j<=pn && i*p[j]<=F; ++j) {
isnp[i*p[j]] = 1;
if(i%p[j] == 0) {
f[i*p[j]] = 1ll * f[i] * s[j] % mod;
break;
}
f[i*p[j]] = 1ll * f[i] * f[p[j]] % mod;
}
}
for (int i=1; i<=F; ++i) {
f[i] = f[i] + f[i-1];
if(f[i] >= mod) f[i] -= mod;
}
}

inline void sol() {
scanf("%d%d", &n, &m);
if(n>m) swap(n, m);
int ans = 0;
for (int i=1, lst; i<=n; i=lst+1) {
lst = min(n/(n/i), m/(m/i));
ans = ans + 1ll * (f[lst] + mod - f[i-1]) * (n/i) % mod * (m/i) % mod;
if(ans >= mod) ans -= mod;
}
printf("%d\n", ans);
}

int main() {
int T; scanf("%d%d", &T, &K);
sieve();
while(T--) sol();
return 0;
}
View Code

posted @ 2017-04-27 17:40  Galaxies  阅读(430)  评论(0编辑  收藏  举报