洛谷 P2257 YY的GCD

 

Code:

#pragma GCC optimize(3)
#pragma GCC optimize(2)
#include <map>
#include <set>
// #include <array>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
// #include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

#define Time (double)clock() / CLOCKS_PER_SEC

#define sd(a) scanf("%d", &a)
#define sdd(a, b) scanf("%d%d", &a, &b)
#define slld(a) scanf("%lld", &a)
#define slldd(a, b) scanf("%lld%lld", &a, &b)

const int N = 1e7 + 20;
const int M = 1e5 + 20;
const int mod = 1e9 + 7;
const double eps = 1e-6;

ll cnt = 0, primes[N], phi[N], sum[N] = {0}, mu[N];
map<ll, ll> su;
bool st[N];

int n, m, p, k;

void get(ll n){
    // phi[1] = 1;
    mu[1] = 1;
    for(ll i = 2; i <= n; i ++){
        if(!st[i]){
            primes[cnt ++] = i;
            // phi[i] = i - 1;
            mu[i] = -1;
        }
        for(int j = 0; primes[j] <= n / i; j ++){
            st[i * primes[j]] = true;
            if(i % primes[j] == 0){
                // phi[i * primes[j]] = primes[j] * phi[i];
                mu[i * primes[j]] = 0;
                break;
            }
            // phi[i * primes[j]] = (primes[j] - 1) * phi[i];
            mu[i * primes[j]] = - mu[i];
        }
    }
}

void init(int n){
    for(int i = 0; i < cnt; i ++){
        int p = primes[i];
        for(int j = p; j <= n; j += p){
            sum[j] += mu[j / p];
        }
    }
    for(int i = 2; i <= n; i ++){
        sum[i] += sum[i - 1];
    }
}


void solve()
{
    sdd(n, m);
    ll ans = 0;
    if(n > m) swap(n, m);
    for(int l = 1, r; l <= n; l = r + 1){
        r = min(n / (n / l), m / (m / l));
        ll res = (ll)(n / l) * (m / l), mid = sum[r] - sum[l - 1];
        ans += res * mid;
    }

    printf("%lld\n", ans);

}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("/home/jungu/code/in.txt", "r", stdin);
    // freopen("/home/jungu/code/out.txt", "w", stdout);
    // freopen("/home/jungu/code/practice/out.txt","w",stdout);
#endif
    // ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T = 1;
    sd(T);
    get(10000000);
    // k = (int)pow(10000000, 0.666667);
    init(10000000);
    // int cas = 1;
    while (T--)
    {
        // printf("Case #%d:", cas++);
        solve();
    }

    return 0;
}

 

posted @ 2020-08-18 21:43  君顾  阅读(111)  评论(0编辑  收藏  举报