标签:sum ext += logs bbb lld color ack display
1个数N(N <= 10^9)
公约数之和
6
15$ans=\sum_{i=1}^ngcd\left(i,n\right)$
$=\sum_{i=1}^n\sum_{j\mid i,j\mid n}\phi\left(j\right)$
$=\sum_{i\mid n}\frac{n\phi\left(i\right)}{i}$
然后$O(\sqrt{n})$的时间枚举因子,暴力求欧拉函数值即可
#include <cstdio> int phi(int n){ int ans = n; for(int i = 2; i * i <= n; i++){ if(n % i == 0){ ans -= ans / i; while(n % i == 0) n /= i; } } if(n != 1) ans -= ans / n; return ans; } int main(){ int n; long long ans = 0; scanf("%d", &n); for(int i = 1; i * i <= n; i++){ if(n % i == 0){ ans += (long long) n * phi(i) / i; if(i * i != n) ans += (long long) n * phi(n / i) * i / n; } } printf("%lld\n", ans); return 0; }
标签:sum ext += logs bbb lld color ack display
原文地址:http://www.cnblogs.com/ruoruoruo/p/7701160.html