前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2478 Farey Sequence

POJ 2478 Farey Sequence

作者头像
全栈程序员站长
发布2022-07-12 15:06:01
1290
发布2022-07-12 15:06:01
举报

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

Description

The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}

You task is to calculate the number of terms in the Farey sequence Fn.

Input

There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 10 6). There are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, you should output one line, which contains N(n) —- the number of terms in the Farey sequence Fn.

Sample Input

代码语言:javascript
复制
2
3
4
5
0

Sample Output

代码语言:javascript
复制
1
3
5
9

打表使用euler函数公式,注意当中巧妙的使用筛子的方法。

代码语言:javascript
复制
const int MAX_SZIE = 1000001;
__int64 phi[MAX_SZIE];
void eulerPhi()
{
	memset (phi, 0, sizeof(phi));
	for (int i = 2; i < MAX_SZIE; i++)
	{
		if (!phi[i])
		{
			for (int j = i; j < MAX_SZIE; j += i)
			{
				if (!phi[j]) phi[j] = j;
				phi[j] = phi[j] / i * (i - 1);
			}
		}
	}
	for (int i = 3; i < MAX_SZIE; i++)
	{
		phi[i] += phi[i-1];
	}
}

int main()
{
	eulerPhi();
	int n;
	while (scanf("%d", &n) && n)
	{
		printf("%lld\n", phi[n]);
	}
	return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118915.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年11月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档