【bzoj2705】 SDOI2012—Longge的问题

http://www.lydsy.com/JudgeOnline/problem.php?id=2705 (题目链接)

题意

  给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N)。

Solution

  完了完了,复杂度分析都不会了。。

  $${ans=\sum_{d|n}d*φ(n/d)}$$

细节

  注意n要开LL

代码

// bzoj2705
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define MOD 10000
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
 
LL n;

LL phi(LL x) {
	LL t=x;
	for (LL i=2;i<=sqrt(x);i++) if (x%i==0) {
			t=t/i*(i-1);
			while (x%i==0) x/=i;
		}
	if (x>1) t=t/x*(x-1);
	return t;
}
int main() {
	scanf("%lld",&n);
	LL ans=0;
	for (int i=1;i<=sqrt(n);i++) if (n%i==0) {
			ans+=i*phi(n/i);
			if (n/i!=i) ans+=(n/i)*phi(i);
		}
	printf("%lld",ans);
    return 0;
}

  

posted @ 2016-12-13 11:34  MashiroSky  阅读(237)  评论(0编辑  收藏  举报