POJ_2478 Farey Sequence 【欧拉函数+简单递推】
一、题目
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
Output
Sample Input
2
3
4
5
0
Sample Output
1
3
5
9
二、题意分析
题意就是给你一个范围内的正整数N,让你去用1~N的数字去组合成Farey序列。关于Farey序列,依题意可知,就是1~N的数字中互素的a,b,其中a<b,就可以凑成一个a/b。然后问有多少个不同的a/b。
我们先看2,就一个,看3,发现2有的3肯定有,然后其余的就是与3互质的数与3凑成的a/b。再看4,3有的还是有,然后再加上与4互质的数与4凑成的a/b。依次递推下去。就是欧拉函数的前N项和。用一个数组累加保存下来,就是所有结果了,再用线性筛法去求欧拉函数(可以看我之前的欧拉函数学习笔记),速度绝对够。需要注意的是,结果增长的很快,需要用long long。
三、AC代码
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1e6+5;
int Phi[MAXN], Prime[MAXN], nPrime;
long long Ans[MAXN]; void Euler()
{
memset(Phi, 0, sizeof(Phi));
Phi[1] = 1;
nPrime = 0;
for(int i = 2; i < MAXN; i++)
{
if(!Phi[i]) //i为素数
{
Phi[i] = i - 1;
Prime[nPrime++] = i;
}
for(int j = 0; j < nPrime && (long long)i*Prime[j] < MAXN; j++)
{
if(i%Prime[j])
{ Phi[ i*Prime[j] ] = Phi[i]*(Prime[j]-1);
}
else
{
Phi[ i*Prime[j] ] = Phi[i]*Prime[j];
break;
}
}
}
return;
} void solve()
{
Euler();
Ans[2] = Phi[2];
for(int i = 3; i < MAXN; i++)
{
Ans[i] = Ans[i-1] + Phi[i];
}
return;
} int main()
{
int N;
solve();
while(cin>>N && N)
{
cout << Ans[N] << endl;
}
return 0;
}
POJ_2478 Farey Sequence 【欧拉函数+简单递推】的更多相关文章
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- POJ2478 Farey Sequence —— 欧拉函数
题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K To ...
- hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数
hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...
- poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)
http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...
- poj2478 Farey Sequence 欧拉函数的应用
仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值 的总和,为什么呢,因为要构成 a/b 然后不能约分 所以 gcd(a,b)==1,所以 分母 b的 欧拉函数值 ...
- UVA12995 Farey Sequence [欧拉函数,欧拉筛]
洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- uva 11426 线性欧拉函数筛选+递推
Problem J GCD Extreme (II) Input: Standard Input Output: Standard Output Given the value of N, you w ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
随机推荐
- ubuntu18.04 按住只能删除一个字符bug
只需要打开重复按键就可以了
- python文件处理os模块
一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.(一语中的) 二.常用方法 1.os.name 输出字符串指示正在使用的平台 ...
- 关于wamp中升级PHP+Apache 的问题
首先个人不建议wamp中升级php版本,如果你不信可以试一试,当你php升级后发想,奥,Apache版本不匹配,然后又去升级Apache,结果搞了半天,弄出来了就好,要是没出来,可能你会气死(好吧,气 ...
- Part7-时钟初始化_lesson1
1.概念解析 1.1时钟脉冲信号 1.2时钟脉冲频率 1.3时钟源(提供时钟脉冲信号) a.晶振 b.锁相环PLL 2.时钟体系 2440: 晶振的频率.时钟体系有多少个PLL.这些PLL分别产生了哪 ...
- fDDA
fDDA:fast DDA,快速的动态数据认证 中国余数定理模式: 就是中国的古人发明的多项式除以多项式的结果的一个定理 AC:应用密文,application cryptogram(密文) gene ...
- Which Uri Encoding method should i use in C#/.net?
June 19, 2015 This too is one of the boring "factual" posts. Sorry Lachlan. I never know w ...
- xen创建pvm和hvm的过程
these are the basic steps of installing domU with xen-tools in ubuntu13.04 64bit in xen4.3 you can a ...
- javascript总结11:JavaScript的自增自减
1 自增自减 1.1 自增写法i++ 作用:在不参与运算的情况下,i++和++i都是在变量的基础加1 var n1 =123; //n1++ 等价于 n1 = n1 +1; ++n1 //等价于 n1 ...
- css总结13:CSS 伪类(Pseudo-classes)
1 伪类作用:CSS伪类是用来添加一些选择器的特殊效果. 2 常用示例: 2.1anchor伪类:代码: 正常语法: a{color:#FF0000;}/* 文字颜色 */ 伪类语法: a:l ...
- exe文件停止运行的情况
1.程序问题. 2.服务器问题. 3.内存占用问题. 一般情况下,关掉程序,重新打开就可以. 上述情况不行,则关掉电脑,重启. 再不行,Ctr + Alt + Del关掉程序的进程. 不行, Win ...