题目链接
题意
求
∑i=1n∑j=1mi2j2gcd(i,j)
(题面上写 n≤106 ,事实上是 n≤1e6 .
十分感谢 WuBaizhe,不然我就一直RE死不瞑目了…
莫比乌斯反演这块也是一篇一篇看着 WuBaizhe的blog学的,对初学者十分友好,每篇的推导都很详细,非常感谢原Po)
初级版(280ms)
推导
令
gcd(i,j)=k
,则
gcd(ik,jk)=1
,则
原式=∑i=1n∑j=1mi2j2k[gcd(ik,jk)=1]=∑k=1min(n,m)k∑i=1⌊nk⌋∑j=1⌊mk⌋(ik)2(jk)2[gcd(i,j)=1]=∑k=1min(n,m)k5∑d=1min(⌊nk⌋,⌊mk⌋)μ(d)∑i=1⌊nkd⌋∑j=1⌊mkd⌋(id)2(jd)2=∑k=1min(n,m)k5∑d=1min(⌊nk⌋,⌊mk⌋)d4μ(d)∑i=1⌊nkd⌋i2∑j=1⌊mkd⌋j2
O(n) 线性筛出 d4μ(d) ,算出 k5 与 i2 的前缀和,再两次分块 n√∗n√=n 就能搞定。
预处理 O(n) ,每组数据 O(n) 。
Code
#include <cstdio>
#include <iostream>
#define maxn 1000010
#define maxm maxn + 10
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
bool check[maxm];
int prime[maxm], kas;
LL h[maxm], pre[maxm], sum5[maxm], sum2[maxm];
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
void init() {
h[1] = 1; int tot = 0;
for (int i = 2; i <= maxn; ++i) {
if (!check[i]) {
prime[tot++] = i;
h[i] = (-poww(i, 4) + mod) % mod;
}
for (int j = 0; j < tot; ++j) {
if (i * prime[j] > maxn) break;
check[i * prime[j]] = true;
if (i % prime[j] == 0) {
h[i * prime[j]] = 0;
break;
}
h[i * prime[j]] = (-h[i] * poww(prime[j], 4) % mod + mod) % mod;
}
}
for (int i = 1; i <= maxn; ++i) {
pre[i] = (pre[i - 1] + h[i]) % mod;
sum5[i] = (sum5[i - 1] + poww(i, 5) % mod) % mod;
sum2[i] = (sum2[i - 1] + poww(i, 2) % mod) % mod;
}
}
LL calc(int a, int b) {
int lim = min(a, b), le, ri;
LL ret = 0;
for (int i = 1; i <= lim; i = ri + 1) {
le = i, ri = min(a / (a / i), b / (b / i));
ret = (ret + (pre[ri] - pre[le - 1] + mod) * sum2[a / i] % mod * sum2[b / i] % mod + mod) % mod;
}
return ret;
}
void work() {
int n, m;
scanf("%d%d", &n, &m);
int lim = min(n, m), le, ri;
LL ans = 0;
for (int i = 1; i <= lim; i = ri + 1) {
le = i, ri = min(n / (n / i), m / (m / i));
ans = (ans + (sum5[ri] - sum5[le - 1] + mod) % mod * calc(n / i, m / i) % mod + mod) % mod;
}
printf("Case #%d: %lld\n", ++kas, ans);
}
int main() {
init();
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}
升级版(96ms)
推导
对上面的式子继续推导,令
T=kd
,
上式=∑k=1min(n,m)k5∑d=1min(⌊nk⌋,⌊mk⌋)d4μ(d)∑i=1⌊nkd⌋i2∑j=1⌊mkd⌋j2=∑T=1min(n,m)∑i=1⌊nT⌋i2∑j=1⌊mT⌋j2∑d|T(Td)5d4μ(d)=∑T=1min(n,m)∑d|TT5dμ(d)∑i=1⌊nT⌋i2∑j=1⌊mT⌋j2
令 f(T)=∑d|TT5dμ(d) ,显然也是可以 O(n) 线性筛出的,接下来再一次分块就行。
预处理 O(n) ,每组数据 O(n√) 。
Code
#include <cstdio>
#include <iostream>
#define maxn 1000010
#define maxm maxn + 10
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
bool check[maxm];
int prime[maxm], kas;
LL h[maxm], pre[maxm], sum5[maxm], sum2[maxm];
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
void init() {
h[1] = 1; int tot = 0;
for (int i = 2; i <= maxn; ++i) {
if (!check[i]) {
prime[tot++] = i;
h[i] = (poww(i, 5) - poww(i, 4) + mod) % mod;
}
for (int j = 0; j < tot; ++j) {
if (i * prime[j] > maxn) break;
check[i * prime[j]] = true;
if (i % prime[j] == 0) {
h[i * prime[j]] = h[i] * poww(prime[j], 5) % mod;
break;
}
h[i * prime[j]] = h[i] * h[prime[j]] % mod;
}
}
for (int i = 1; i <= maxn; ++i) {
pre[i] = (pre[i - 1] + h[i]) % mod;
sum2[i] = (sum2[i - 1] + poww(i, 2) % mod) % mod;
}
}
void work() {
int n, m;
scanf("%d%d", &n, &m);
int lim = min(n, m), le, ri;
LL ans = 0;
for (int i = 1; i <= lim; i = ri + 1) {
le = i, ri = min(n / (n / i), m / (m / i));
ans = (ans + (pre[ri] - pre[le - 1] + mod) % mod * sum2[n / i] % mod * sum2[m / i] % mod) % mod;
}
printf("Case #%d: %lld\n", ++kas, ans);
}
int main() {
init();
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}