现在的位置: 首页 > 综合 > 正文

poj 2478 Farey Sequence 欧拉函数

2013年05月27日 ⁄ 综合 ⁄ 共 590字 ⁄ 字号 评论关闭

题目链接:http://poj.org/problem?id=2478

/*
* 题目:
*       求0<a<b<=n中,所有的最简分数a/b的个数
*
* 分析:
*      先化成求sigma( phi(n) ),然后转化为快速求欧拉函数。
*
* */

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>

using namespace std;

const int X = 1000005;

typedef long long ll;

ll ans[X];
bool use[X];

void init(){
	memset(use,false,sizeof(use));
	for(int i=1;i<X;i++)
		ans[i] = i;
	for(int i=2;i<X;i++)
		if(!use[i]){
			for(int j=i;j<X;j+=i){
				use[j] = true;
				ans[j] = ans[j]/i*(i-1);
			}
		}
	ans[2] = 1;
	for(int i=3;i<X;i++)
		ans[i] += ans[i-1];
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("sum.txt","r",stdin);
#endif
	int n;
	init();
	while(scanf("%d",&n),n)
		printf("%lld\n",ans[n]);

	return 0;
}

  

抱歉!评论已关闭.