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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • System setProperty(property,value)方法

    System setProperty(property,value)方法通常JDKd的运行参数设置为#forlinuxexportJAVA_OPTS=”$JAVA_OPTS-Dcode=BeiJing”#forwinsetJAVA_OPTS=%JAVA_OPTS% -Dcode=BeiJing相当于Java代码里面实现System.setProperty(“code”,”BeiJing”);同时,获取code的值则可以用下列

    2022年7月12日
    19
  • windowsAPI之OpenProcessToken,AdjustTokenPrivileges 和LookupPrivilegeValue

    windowsAPI之OpenProcessToken,AdjustTokenPrivileges 和LookupPrivilegeValue这三个函数主要用来提升进程的权限1OpenProcessToken()函数:获取进程的令牌句柄OpenProcessToken的原型.BOOLWINAPIOpenProcessToken(__inHANDLEProcessHandle,__inDWORDDesiredAccess,__outPHA

    2022年6月25日
    31
  • 内核杂谈——关于platform device 创建

    内核杂谈——关于platform device 创建当拿到driver,不能用起来的时候需要去检查device了。虽说device和bus通常都是系统中带的,但也不要想当然的认为这个系统是帮你建好的。通常busdevicedriver三者中,bus基本不用干预,device干预的少,driver干预的多。从设备树中生成device从设备树中识别device的入口为arch_initcall_sync(of_platform_default_populate_init);staticint__initof_platform_defa

    2022年7月24日
    14
  • SQL SELECT TOP 子句详解

    SQL SELECT TOP 子句详解SQLSELECTTOP子句SELECTTOP子句用于规定要返回的记录的数目。语法如下SELECTTOPnumber|percentcolumn_name(s)FROMtable_name;实例:从MyTables表里选取前两行数据实例2从MyTables表里取前百分之十的数据需要注意的是如下图那是我整张MyTables表的数据,可以看出根本没有十条数据,那那…

    2022年7月13日
    24
  • Vue项目实战05:18n实现多语言自动切换-浏览器语言设置「建议收藏」

    Vue项目实战05:18n实现多语言自动切换-浏览器语言设置「建议收藏」什么是vue-i18ni18n是Internationalization这个英文的简写,即国际化的意思,vue-i18n是一款针对于vue开发的国际化插件,让项目支持多语言切换,以适应不同地区用户的需求。安装vue-i18n直接在项目中执行安装命令:npminstallvue-i18n–save​全局引入vue-i18n在项目中引入vue-i18n,实例化vue-i18n将需要加载的语言包通过require导入,这里看个人需求我只需要中英日文,所以引入zh-CN.js和en-US.j

    2022年6月4日
    37
  • Angular面试题_angular面试

    Angular面试题_angular面试一、ng-show/ng-hide与ng-if的区别?第一点区别是,ng-if在后面表达式为true的时候才创建这个dom节点,ng-show是初始时就创建了,用display:block和display:none来控制显示和不显示。第二点区别是,ng-if会(隐式地)产生新作用域,ng-switch、ng-include等会…

    2022年8月31日
    5

发表回复

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

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