UVA - 11426 GCD - Extreme (II) (欧拉函数)

Posted duskob

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 11426 GCD - Extreme (II) (欧拉函数)相关的知识,希望对你有一定的参考价值。

思路

将题意转化为$sum_{i = 1}^{n} sum_{j = 1}^{i - 1}gcd(i, j)$,考虑每个最大公因数的值$k$对答案的影响。假设

$gcd(A,B) = k$

那么肯定可以表示成

$gcd(ak,bk) = k$

$gcd(a, b) = 1$

假设$a>b$那么$b$有$varphi (a)$种选法且$ak leq n$等价于$a leq left lfloor frac{n}{k} ight floor$,因此对于一种$k$,它对答案的贡献为

$sum_{a = 2}^{left lfloor frac{n}{k} ight floor} k*varphi (a)$

$k sum_{a = 1}^{left lfloor frac{n}{k} ight floor} varphi (a) - k$

$k (sum_{a = 1}^{left lfloor frac{n}{k} ight floor} varphi (a) - 1)$

所以

$ans = sum_{k = 1}^{n} k (sum_{a = 1}^{left lfloor frac{n}{k} ight floor} varphi (a) - 1)$

维护欧拉函数前缀和之后整除分块,时间复杂度$O(n+sqrt{n})$

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 1e7 + 5;

int n;
int tag[N], pri[N], cnt;
LL phi[N];
LL pre[N];

void getPrime() {
    phi[1] = 1;
    for(int i = 2; i < N; i++) {
        if(!tag[i]) {
            pri[cnt++] = i;
            phi[i] = i - 1;
        }
        for(int j = 0; j < cnt && i * pri[j] < N; j++) {
            tag[i * pri[j]] = 1;
            if(i % pri[j] == 0) {
                phi[i * pri[j]] = phi[i] * pri[j];
                break;
            }
            phi[i * pri[j]] = phi[i] * (pri[j] - 1);
        }
    }

    for(int i = 1; i < N; i++) pre[i] = pre[i - 1] + phi[i];
}

int main() {
    getPrime();
    while(~scanf("%d", &n) && n != 0) {
        LL ans = 0;
        for(LL l = 1, r; l <= n; l = r + 1) {
            r = 1LL * n / (1LL * n / l);
            LL tmp1 = ((r + l) * (r - l + 1)) / 2LL;
            LL tmp2 = pre[n / l] - 1LL;
            ans += tmp1 * tmp2;
        }
        printf("%lld
", ans);
    }
    return 0;
}
/*
10
100
200000
0
*/

 

 

以上是关于UVA - 11426 GCD - Extreme (II) (欧拉函数)的主要内容,如果未能解决你的问题,请参考以下文章

UVa11426 GCD - Extreme (II)

UVA - 11426 GCD - Extreme (II) (欧拉函数)

GCD - Extreme (II) UVA - 11426(欧拉函数!!)

UVA 11426gcd之和 (改编)

UVA 11426 (欧拉函数&&递推)

gcd(1,n)+gcd(2,n)....gcd(n-1,n); Uva11426