[HAOI2011]Problem b 题解

Posted linzhengmin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HAOI2011]Problem b 题解相关的知识,希望对你有一定的参考价值。

前言

WA了两次,结果发现打容斥的时候加号打成减号了...
其实这题还是挺简单的

题解

如何计算\\(1 \\leq x \\leq a\\)\\(1 \\leq y \\leq b\\)\\(gcd(x,y)=d\\)是这题的简化版
给出题解
然后发现这题就是那道题加一个容斥原理
我们要求的答案就是\\(ans(b,d)-ans(b,c-1)-ans(a-1,d)+ans(a-1,c-1)\\)
小学生都会证啊
于是愉快的AC了

代码

#include <cstdio>
#include <algorithm>
#define ll long long
#define MAXNUM 50005

int mu[MAXNUM], is_not_prime[MAXNUM], primes[MAXNUM / 10], prime_num;
// prefix
ll qzh[MAXNUM];

int read()
    int x = 0; int zf = 1; char ch = ' ';
    while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    if (ch == '-') zf = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;


void init()
    mu[1] = 1; is_not_prime[0] = is_not_prime[1] = 1;
    for (int i = 2; i <= MAXNUM; ++i)
        if (!is_not_prime[i]) mu[primes[++prime_num] = i] = -1;
        for (int j = 1; j <= prime_num && primes[j] * i <= MAXNUM; ++j)
            is_not_prime[i * primes[j]] = 1;
            if (!(i % primes[j])) break;
            else
                mu[primes[j] * i] = -mu[i];
        
    
    for (int i = 1; i <= MAXNUM; ++i)
        qzh[i] = qzh[i - 1] + mu[i];


ll solve(int n, int m, int d)
    if (n == 0 || m == 0) return 0;
    ll ans = 0;
    n /= d, m /= d;
    if (n > m) n ^= m ^= n ^= m; ans = 0;
    for (int l = 1, r; l <= n; l = r + 1)
        r = std::min(n / (n / l), m / (m / l));
        ans += (ll)(qzh[r] - qzh[l - 1]) * (n / l) * (m / l);
    
    return ans;


int main()
    init();
    int T = read(), a, b, n, m, d; ll ans;
    while (T--)
        a = read(), n = read(), b = read(), m = read(), d = read();
        ans = solve(n, m, d) - solve(n, b - 1, d) - solve(a - 1, m, d) + solve(a - 1, b - 1, d);
        printf("%lld\\n", ans);
    
    return 0;

以上是关于[HAOI2011]Problem b 题解的主要内容,如果未能解决你的问题,请参考以下文章

P2522 [HAOI2011]Problem b 题解

[HAOI2011]Problem b 题解

bzoj 2301 HAOI2011 Problem b

P2522 [HAOI2011]Problem b

bzoj2298: [HAOI2011]problem a

bzoj2301HAOI2011Problem b