luogu P3455 (Mobius inversion)

Meaning of the questions:

Find x in [1, n] range, y in [1, m] in the range satisfying gcd (x, y) = k x, y, logarithmic.

answer:
  • That question is slightly different with the GCD. First, this question (x, y) (y, x) think differently.
  • This question is to use a block of skill.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;

int T, k, n, m;
int pri[N], cnt;
int mu[N];
int vis[N];
int sum[N];

void prime(){
    mu[1] = 1;
    sum[1] = 1;
    for(int i = 2; i <= 4e5; ++ i){
        if(!vis[i]){
            pri[++ cnt] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= cnt && pri[j] * i <= 4e5; ++ j){
            vis[pri[j] * i] = 1;
            if(i % pri[j] == 0){
                mu[pri[j] * i] = 0;
                break;
            }
            mu[pri[j] * i] = -mu[i];
        }
        sum[i] = sum[i - 1] + mu[i];
    }
}


int main()
{
    prime();
    scanf("%d",&T);
    for(int Case = 1; Case <= T; ++ Case){
        scanf("%d%d%d", &n, &m, &k);
        if(!k){
            printf("0\n", Case);
            continue;
        }
        n /= k, m /= k;
        if(n > m) swap(n, m);
        ll ans = 0;
        for(int i = 1, last = 1; i <= n; i = last + 1){
            last = min(n / (n / i), m / (m / i));				
            ans += (ll)(sum[last] - sum[i - 1]) * (n / i) * (m / i);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Problem solution blog: https://blog.csdn.net/litble/article/details/72804050?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

Guess you like

Origin www.cnblogs.com/A-sc/p/12626492.html