NJU 1017 [JSCPC2016]Heresy 莫比乌斯反演

题目链接

题意

i=1nj=1mi2j2gcd(i,j)

(题面上写 n106 ,事实上是 n1e6 .
十分感谢 WuBaizhe,不然我就一直RE死不瞑目了…
莫比乌斯反演这块也是一篇一篇看着 WuBaizhe的blog学的,对初学者十分友好,每篇的推导都很详细,非常感谢原Po)

初级版(280ms)

推导

gcd(i,j)=k ,则 gcd(ik,jk)=1 ,则

=i=1nj=1mi2j2k[gcd(ik,jk)=1]=k=1min(n,m)ki=1nkj=1mk(ik)2(jk)2[gcd(i,j)=1]=k=1min(n,m)k5d=1min(nk,mk)μ(d)i=1nkdj=1mkd(id)2(jd)2=k=1min(n,m)k5d=1min(nk,mk)d4μ(d)i=1nkdi2j=1mkdj2

O(n) 线性筛出 d4μ(d) ,算出 k5 i2 的前缀和,再两次分块 nn=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)k5d=1min(nk,mk)d4μ(d)i=1nkdi2j=1mkdj2=T=1min(n,m)i=1nTi2j=1mTj2d|T(Td)5d4μ(d)=T=1min(n,m)d|TT5dμ(d)i=1nTi2j=1mTj2

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值