• Luogu P2303 [SDOI2012] Longge 的问题


    gate

    (sumlimits_{i=1}^{n}gcd(i,n))

    (=sumlimits_{d=1}^{n}d sumlimits_{i=1}^{n}[gcd(i,n)=d])

    (=sumlimits_{d=1}^{n}d sumlimits_{i=1}^{frac{n}{d}} [gcd(i,frac{n}{d})=1](d|i,d|n))

    考虑欧拉函数的定义,(varphi(x) = sumlimits_{i=1}^{x}[gcd(i,x)=1])

    (=sumlimits_{d=1}^{n} d imesvarphi(frac{n}{d})(d|n))

    (=sumlimits_{d|n} d imesvarphi(frac{n}{d}))

    枚举(x)的因数(d),再枚举(d)的质因子求欧拉函数即可。

    code

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #define MogeKo qwq
    using namespace std;
    
    long long n;
    
    long long phi(long long x) {
    	long long ans = x;
    	for(long long i = 2; i*i <= x; i++) {
    		if(x%i == 0) ans = ans / i * (i-1);
    		while(x%i == 0) x /= i;
    	}
    	if(x > 1) ans = ans / x * (x-1);
    	return ans;
    }
    
    long long f(long long x) {
    	long long ans = 0;
    	for(long long i = 1; i*i <= x; i++)
    		if(x%i == 0) {
    			ans += i*phi(x/i);
    			if(i*i != x) ans += (x/i)*phi(i);
    		}
    	return ans;
    }
    
    int main() {
    	scanf("%lld",&n);
    	printf("%lld",f(n));
    	return 0;
    }
    
  • 相关阅读:
    google搜索教程
    phoenix/stack-five
    phoenix/stack-four
    phoenix/stack-three
    phoenix/stack-one
    fork 在 Linux 内核里面的实现
    无文件执行 ELF
    glibc 堆内存管理杂记
    QOS shaping 知识要点
    QOS CQ
  • 原文地址:https://www.cnblogs.com/mogeko/p/13338978.html
Copyright © 2020-2023  润新知