(模板题)UVALive 7362 Farey(欧拉函数)
来源:互联网 发布:xp映射网络驱动器 编辑:程序博客网 时间:2024/04/19 13:49
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5384
Given a positive integer, N, the sequence of all fractions a/b with (0 < a ≤ b), (1 < b ≤ N) and a andb relatively prime, listed in increasing order, is called the Farey Sequence of order N.
For example, the Farey Sequence of order 6 is:
0/1, 1/6, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 5/6, 1/1
For this problem, you will write a program to compute the length of the Farey sequence of order N(input).
InputThe first line of input contains a single integer P, (1 ≤ P ≤ 10000), which is the number of data setsthat follow.
Each data set should be processed identically and independently.Each data set consists of a single line of input.
It contains the data set number, K, followed by theorder N, N(2 ≤ N ≤ 10000), of the Farey Sequence whose length is to be found.
OutputFor each data set there is a single line of output. The single output line consists of the data set number,K, followed by a single space followed by the length of the Farey Sequence as a decimal integer.
Sample Input
4
1 6
2 15
3 57
4 9999
Sample Output
1 13
2 73
3 1001
4 30393487
提示
题意:有p(1<=p<=10000)组数据,给出一个数n(2<=n<=10000),请求出有几种写成分数的情况。
分数格式:
a/b,且a<b<=n,1/2,2/4,算一种情况,换句话说,a与b两两互质(最大公约数为1)。
思路:
不晓得欧拉函数的只能眼睁睁看人家AC刷刷的往上冒,心痛啊%>_<%。
直接套就行了。
示例程序
#include <stdio.h>int f(int n){ int t=1,i; for(i=2;n>=i*i;i++) { if(n%i==0) { n=n/i; t=t*(i-1); while(n%i==0) { n=n/i; t=t*i; } } } if(n>1) { t=t*(n-1); } return t;}int main(){ int b[10001],i,i1,n,m,t; b[1]=2; for(i=2;10000>=i;i++) { b[i]=b[i-1]+f(i); } scanf("%d",&t); for(i=1;t>=i;i++) { scanf("%d %d",&m,&n); printf("%d %d\n",m,b[n]); } return 0;}
下面一种做法(看上去应该差不多)的是其他人的一份代码:
#include <stdio.h>int main(){ int a[10001],b[10001],i,i1,n,m,t; a[1]=2; b[1]=2; for(i=2;10000>=i;i++) { a[i]=i-1; for(i1=2;i>i1;i1++) { if(i%i1==0) { a[i]=a[i]-a[i1]; } } b[i]=b[i-1]+a[i]; } scanf("%d",&t); for(i=1;t>=i;i++) { scanf("%d %d",&m,&n); printf("%d %d\n",m,b[n]); } return 0;}
- (模板题)UVALive 7362 Farey(欧拉函数)
- UVALive 7362 Farey (欧拉函数)
- Farey Sequence+欧拉函数打表+模板题+poj
- poj_2478-Farey Sequence(欧拉函数) 【模板】
- POJ 2478 Farey Sequence(Farey数列&欧拉函数求和)
- [数学-欧拉函数]Pku2478--Farey Sequence
- 2478 Farey Sequence //欧拉函数
- 1089. Farey Sequence (欧拉函数+筛法)
- POJ2478 Farey Sequence,欧拉函数
- poj2407 Farey Sequence 欧拉函数
- poj 2478 Farey Sequence (欧拉函数)
- poj 2478 Farey Sequence(欧拉函数)
- POJ 2478 Farey Sequence(欧拉函数)
- POJ 2478 : Farey Sequence - 欧拉函数
- poj 2478 Farey Sequence(欧拉函数)
- Farey Sequence(欧拉函数)
- poj2478 Farey Sequence (欧拉函数)
- POJ 2478 Farey Sequence 欧拉函数 .
- yii框架里ajax分页和搜索 单删 批删
- 并发与并行的区别
- 内存管理:01存储器层次结构
- 文章标题
- nRF2401A无线传输模块介绍
- (模板题)UVALive 7362 Farey(欧拉函数)
- RN捕获异常
- [已解决]error: possibly undefined macro: AC_PROG_LIBTOOL
- Leetcode 27 Remove Element
- Ubuntu 16.04 一系列软件安装命令,包括QQ、搜狗、Chrome、vlc、网易云音乐安装方法
- 自定义搜索框
- 第五周 求两整数正差值
- 知识竞赛时,倒计时字体的大小和位置如何调整?
- Jenkins+maven+svn+tomcat操作手册(一)