• [洛谷2257]ZAP-Queries 题解


    前言

    这道题还是比较简单的

    解法

    首先将题目转化为数学语言。
    题目要我们求的是:

    [sum_{i=1}^asum_{j=1}^b[gcd(i,j)=d] ]

    按照套路1,我们将其同时除以d转换为

    [sum_{i=1}^{lfloorfrac{a}{d} floor}sum_{j=1}^{lfloorfrac{b}{d} floor}[gcd(i,j)=1] ]

    按照技巧1,我们将其变换为

    [sum_{i=1}^{lfloorfrac{a}{d} floor}sum_{j=1}^{lfloorfrac{b}{d} floor}sum_{x|gcd(i,j)}mu(x) ]

    按照技巧3,我们将其变换为

    [sum_{i=1}^{lfloorfrac{a}{d} floor}sum_{j=1}^{lfloorfrac{b}{d} floor}sum_{x=1}^{lfloorfrac{a}{d} floor}mu(x) imes[x|gcd(i,j)] ]

    我们发现要满足(d|gcd(i,j)),我们的i,j必须是d的倍数
    然后我们可以开心地去掉两个(sum)

    [sum_{x=1}^{lfloorfrac{a}{d} floor}mu(x) imeslfloorfrac{a}{xd} floorlfloorfrac{b}{xd} floor ]

    至此化简结束,我们求出(mu)函数的前缀和
    然后我们整除分块,解决问题

    代码

    #include <cstdio>
    #include <algorithm>
    #define ll long long
    #define MAXNUM 500005
    
    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];
    }
    
    int main(){
        init();
        int T = read(), n, m, d; ll ans;
        while (T--){
            n = read(), m = read(), d = read();
            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);
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
    

    P.S. 数组开的好像有点大,不要介意

  • 相关阅读:
    Redis持久化(八)
    Redis通用命令(七)
    Redis数据结构(六)
    Redis数据结构(五)
    Redis数据结构(四)
    Redis数据结构(三)
    nodename nor servname provided, or not known
    edis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is configured to save RDB snapshots,
    Redis与Java的链接Jedis(二)
    高并发Redis(Mac)环境配置(一)
  • 原文地址:https://www.cnblogs.com/linzhengmin/p/11066848.html
Copyright © 2020-2023  润新知