POJ 2478 Farey Sequence

POJ 2478 Farey Sequence

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

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

2
3
4
5
0

Sample Output

1
3
5
9

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

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.net/118915.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MongoDB(六)—-MongoDB索引的额外属性

    MongoDB(六)—-MongoDB索引的额外属性

    2020年11月12日
  • eclipse怎么配置svn插件_dip插件有哪几种

    eclipse怎么配置svn插件_dip插件有哪几种Eclipse安装插件的几种方法(eclipse安装SVN插件)在这里啰嗦一句,其实更新的版本mar2更好用。但是由于在低电压版的本本上,mar2跑不起来,经常假死,kepler可以很流畅。 so,如果为了携带了轻便低电压版CPU的本本,mar2跑不起来,就用kepler吧。以Eclipse(Kepler)安装SVN插件为例,记录eclipse安装插件的几种方法。 1eclipsemarket…

    2022年10月10日
  • pycharm暂停了,如何继续运行_pycharm运行完后不退出运行

    pycharm暂停了,如何继续运行_pycharm运行完后不退出运行pycharm的并行运行在pycharm中想要对一个程序同时多开几个运行窗口,并行的同时运行。但是会弹出下面提示:解决方法:步骤一:在pycharm顶部菜单栏单击Run(运行)->EditConfigurations(编辑配置)步骤二:在弹出的Run/Debugconfigurations窗口的右上角将“Allowparallelrun”打勾即可,如下图所示:不能自动…

    2022年8月29日
  • 使用SchedulerFactoryBean集成Quarz Job与Spring

    使用SchedulerFactoryBean集成Quarz Job与Spring本文介绍QuartzJob与Spring的集成。[list][*]QuartzJob官网:[url]http://quartz-scheduler.org[/url][*]Spring官网:[url]https://spring.io[/url][*][b]SchedulerFactoryBeanJavaDoc:[/b][url]http://docs.spring.i…

    2022年5月24日
  • arm与stm32的关系_STM32系列处理器常见型号与特性

    arm与stm32的关系_STM32系列处理器常见型号与特性ARM介绍1978年,奥地利籍物理学博士HermannHauser,和他朋友,一位英国工程师ChrisCurry,共同创建了一家名为“CambridgeProcessorUnit,CPU”的公司,中文字面意思就是“剑桥处理器单元”,主要从事研发当地电子仪器设备的业务,比如街头游戏机之类的。1979年,在经营逐渐进入正规后,原来的公司名不怎么合适了,那个年代喜欢找个吉祥物作为公司名字,比如Apple公司。这俩就以橡子为吉祥物,取名为“AcronComputer”,中文字面意思就是“橡果公

    2022年10月9日
  • List KeyValuePair

    List KeyValuePairList<KeyValuePair<string,string>>list=newList<KeyValuePair<string,string>>();list.Add(newKeyValuePair<string,string>(“asdf1″,”1”));list.Add(newKeyValuePair&l…

    2022年7月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号