Farey Sequence (欧拉函数+前缀和)
题目链接:http://poj.org/problem?id=2478
Description
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 思路:由a / b,gcd(a,b)=1知,当前f(n)是在f(n - 1)的基础上加上以n为分母,与n互质的数为分子的分数,所以f(n)比f(n - 1)增加了1~n内与n互质的数的个数,现在题意就很明显了,就是要求欧拉函数,不过需要一个前缀和。
代码实现如下:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; #define debug(x) cout <<'[' <<x <<']' <<endl;
const int maxn = 1e6 + ;
int n, m;
int v[maxn], p[maxn], phi[maxn];
long long sum[maxn]; void euler() {
m = ;
memset(v, , sizeof(v));
memset(sum, , sizeof(sum));
for(int i = ; i < maxn; i++) {
if(v[i] == ) {
v[i] = i;
p[m++] = i;
phi[i] = i - ;
}
for(int j = ; j < m; j++) {
if(p[j] > v[i] || p[j] > maxn / i) break;
v[i * p[j]] = p[j];
phi[i * p[j]] = phi[i] * (i % p[j] ? p[j] - : p[j]);
}
}
phi[] = phi[] = ;
for(int i = ; i < maxn; i++) {
sum[i] = sum[i - ] + phi[i];
}
} int main() {
euler();
while(~scanf("%d", &n) && n) {
// debug(phi[n]);
printf("%lld\n", sum[n]);
}
}
Farey Sequence (欧拉函数+前缀和)的更多相关文章
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- UVA12995 Farey Sequence [欧拉函数,欧拉筛]
洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...
- POJ2478 Farey Sequence —— 欧拉函数
题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K To ...
- 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的 欧拉函数值 ...
- hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数
hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
- POJ-2478-Farey Sequence(欧拉函数)
链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...
随机推荐
- ACM 第十天
动态规划2 1.树形DP 2.概率DP 3.区间DP 模板 ; len < n; len++) { //操作区间的长度 , j = len; j <= n; i++, j++) { //始 ...
- 3dContactPointAnnotationTool开发日志(十一)
把image也做成panel的形式了,并且放进了scrollView里,真实地显示出图像: 其它两个scrollView的content也做成自适应大小了,就是添加一项content的heig ...
- matlab的二维卷积操作(转)
MATLAB的conv2函数实现步骤(conv2(A,B)): 其中,矩阵A和B的尺寸分别为ma*na即mb*nb ① 对矩阵A补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都 ...
- 【week6】psp
本周psp
- SQL 中 Date 与Datetime的区别
Date是SQL Server 2008新引进的数据类型.它表示一个日子,不包含时间部分,可以表示的日期范围从公元元年1月1日到9999年12月31日.只需要3个字节的存储空间. DateTime 日 ...
- java中多种方式读文件
转自:http://www.jb51.net/article/16396.htm java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内 ...
- jQuery的动画与特效
显示与隐藏 show() 和 hide() 方法 动画效果的show() 和 hide() show(speed,[]callback) hide(speed,[]callback) speed:表示 ...
- html5 isPointInPath相关操作
<body> <canvas id="> </canvas> <script type="text/javascript"> ...
- stm32f407启动文件分析
; Amount of memory (in bytes) allocated for Stack; Tailor this value to your application needs; < ...
- bzoj2165: 大楼 (矩阵快速幂)
//========================== 蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/ 转载要声明! //=============== ...