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 ...
随机推荐
- Debian常用软件
1. 有道词典 https://github.com/justzx2011/openyoudao
- laravel 可用验证规则
accepted 验证的字段必须为 yes. on. 1.或 true.这在确认服务条款是否同意时相当有用. active_url 相当于使用了 PHP 函数 dns_get_record,验证的字段 ...
- CCleaner专业版注册码
下载软件安装之后: 1.断网 2.用户名:任意,注册码:C2YW-XZT7-A4SE-UD89-YZPC
- 解决XAMPP中,MYSQL因修改my.ini后,无法启动的问题
论这世上谁最娇贵,不是每年只开七天的睡火莲,也不是瑞典的维多利亚公主,更不是一到冬天就自动关机的iPhone 6s, 这世上最娇贵的,非XAMPP中的mysql莫属,记得儿时的我,年少轻狂,当时因为m ...
- 浅述Try {} Catch{} 作用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...
- 2019 front end jobs collection
2019 front end jobs collection Alibaba https://ant.design/docs/spec/work-with-us-cn https://www.yuqu ...
- 关于Axure RP
Axure RP 是一款专业的原型设计工具 用于快速创建应用软件的线框图.流程图.原型和规格说明文档 贴一张图
- Version
题目 有三个操作: \(change \ u \ v \ a \ b\) : \(u\)到\(v\)路径上的点点权加上\(a+k*b\),\(k\)为第几个点,\(u\)为第0个点. \(query ...
- BZOJ4237 稻草人(分治+树状数组+单调栈)
如果要询问的某个纵坐标为inf的点左边是否有点能与其构成所要求的矩形,只要用个单调栈就可以了.可以想到用分治来制造单调性. 按横坐标排序,每次考虑跨过分治中心的矩形.考虑右边的每个点能与左边的哪些点构 ...
- Qt基本控件及三大布局
Qt基本控件及三大布局 来源: http://blog.csdn.net/a2604539133/article/details/73920696 Qt基本模块 一.Qt的三大布局 QHBoxLayo ...