莫比乌斯反演1

题目链接戳这里(洛谷P3455)

题目描述:

给出 a, b, d, 求满足 1 ≤ x ≤ a, 1 ≤ y ≤ b, 且 gcd(x,y)=d 的二元组 (x,y) 的数量。

思路

首先,我们根据题意可以得到下面这个式子:
a n s = ∑ x = 1 a ∑ y = 1 b [ gcd ⁡ ( x , y ) = d ] ans = \sum\limits_{x=1}^a\sum\limits_{y = 1}^b [\gcd(x,y)=d] ans=x=1ay=1b[gcd(x,y)=d]
除掉d,我们得到:
a n s = ∑ x = 1 a / d ∑ y = 1 b / d [ gcd ⁡ ( x , y ) = 1 ] ans = \sum\limits_{x=1}^{a/d}\sum\limits_{y = 1}^{b/d} [\gcd(x,y)=1] ans=x=1a/dy=1b/d[gcd(x,y)=1]
而 [gcd(x,y)=1] 形如 [x = 1]于是我们继续化简:
a n s = ∑ x = 1 a / d ∑ y = 1 b / d ∑ k ∣ g c d ( x , y ) μ ( k ) ans = \sum\limits_{x=1}^{a/d}\sum\limits_{y = 1}^{b/d}\sum\limits_{k|gcd(x,y)} μ(k) ans=x=1a/dy=1b/dkgcd(x,y)μ(k)
交换求和顺序:
a n s = ∑ k ∣ g c d ( x , y ) μ ( k ) ∑ x = 1 a / d ∑ y = 1 b / d 1 ans = \sum\limits_{k|gcd(x,y)} μ(k)\sum\limits_{x=1}^{a/d}\sum\limits_{y = 1}^{b/d}1 ans=kgcd(x,y)μ(k)x=1a/dy=1b/d1
我们从k = 1开始枚举 k:
a n s = ∑ k = 1 m i n ( a , b ) / d μ ( k ) ∗ ⌊ a d k ⌋ ∗ ⌊ b d k ⌋ ans = \sum\limits_{k=1}^{min(a, b)/d} μ(k)*⌊\frac{a}{dk}⌋ * ⌊\frac{b}{dk}⌋ ans=k=1min(a,b)/dμ(k)dkadkb
结束了。
按照整数分块的思想来完成代码,提前处理 mu[i] (莫比乌斯函数) 的前缀和。

代码如下:

//Siberian Squirrel
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
typedef long long ll;
#define printd(x) printf("%d\n", x)
#define printlld(x) printf("%lld\n", x)
#define printlf(x) printf("%.3f\n", x)
#define rep(i, l, r) for(int i = l, i##end = r; i <= i##end; ++i)
#define per(i, l, r) for(int i = l, i##end = r; i >= i##end; --i)
const int inf = 0x3f3f3f3f, MAXN = 5e4 + 10, MOD = 1000000007;
const double PI = acos(-1);
inline int read() {
    int res = 0;
    char c = getchar();
    while (!isdigit(c)) c = getchar();
    while (isdigit(c)) res = (res << 1) + (res << 3) + (c ^ 48), c = getchar();
    return res;
}
inline int ADD(int a, int b) {
    return (1ll * a + b) % MOD;
}
inline int MUL(int a, int b) {
    return 1ll * a * b % MOD;
}
inline int SUB(int a, int b) {
    return (a - b) < 0? (a - b + MOD) % MOD: a - b;
}
//
int a, b, d;
int prime[MAXN], mu[MAXN], cnt = 0, qianzhui[MAXN];
bool vis[MAXN];
//
inline void pre(int n) {
    mu[1] = 1;
    for(int i = 2; i <= n; ++i) {
        if(!vis[i]) prime[++cnt] = i, mu[i] = -1;
        for(int j = 1; j <= cnt && i * prime[j] <= n; ++j) {
            vis[i * prime[j]] = true;
            if(i % prime[j] == 0) {
                mu[i * prime[j]] = 0;
                break;
            } else mu[i * prime[j]] = -mu[i];
        }
    }
    qianzhui[0] = 0;
    for(int i = 1; i <= n; ++i) qianzhui[i] = qianzhui[i - 1] + mu[i];
}
void solve(ll res = 0) {
    pre(MAXN - 2);
    int T = read();
    while(T--) {
        res = 0;
        a = read(), b = read(), d = read();
        a /= d, b /= d;
        if(a < b) swap(a, b);
        for(int i = 1, j; i <= b; i = j + 1) {
            j = min(a / (a / i), b / (b / i));
            res += (qianzhui[j] - qianzhui[i - 1]) * 1ll * (a / i) * (b / i);
        }
        printlld(res);
    }
}
int main() {
#ifdef ACM_LOCAL
    signed test_index_for_debug = 1;
	char acm_local_for_debug = 0;
	do {
		if (acm_local_for_debug == '$') exit(0);
		if (test_index_for_debug > 20)
			throw runtime_error("Check the stdin!!!");
		double start_clock_for_debug = clock();
		solve();
		double end_clock_for_debug = clock();
		cout << "Test " << test_index_for_debug << " successful" << endl;
		cerr << "Test " << test_index_for_debug++ << " Run Time: "
		     << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
		cout << "--------------------------------------------------" << endl;
	} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值