Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14291   Accepted: 5647

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 <= 106). 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

Source

POJ Contest,Author:Mathematica@ZSU
 
题解:
直接欧拉函数,求个前缀和,就好了。
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
LL qz[];
int phi[],prime[],tot;
bool vis[];
void Eular()
{
int i,j;
phi[]=;tot=;
for(i=;i<=;i++)
{
if(vis[i]==false)
{
prime[++tot]=i;
phi[i]=i-;
}
for(j=;j<=tot&&prime[j]*i<=;j++)
{
vis[prime[j]*i]=true;
if(i%prime[j]==)
{
phi[prime[j]*i]=phi[i]*prime[j];
break;
}
phi[prime[j]*i]=phi[prime[j]]*phi[i];
}
}
}
void Qz()
{
for(int i=;i<=;i++)qz[i]=qz[i-]+phi[i];
}
int main()
{
int n;
Eular();
//for(int i=1;i<=100;i++)printf("%d ",phi[i]);
//printf("\n");
Qz();
while()
{
scanf("%d",&n);
if(n==)break;
printf("%lld\n",qz[n]-);
}
return ;
}

Poj 2478-Farey Sequence 欧拉函数,素数,线性筛的更多相关文章

  1. poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)

    http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...

  2. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  3. poj 2478 Farey Sequence 欧拉函数前缀和

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       Description The Farey Sequence Fn for ...

  4. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  5. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  6. 洛谷UVA12995 Farey Sequence(欧拉函数,线性筛)

    洛谷题目传送门 分数其实就是一个幌子,实际上就是求互质数对的个数(除开一个特例\((1,1)\)).因为保证了\(a<b\),所以我们把要求的东西拆开看,不就是\(\sum_{i=2}^n\ph ...

  7. poj2478 Farey Sequence 欧拉函数的应用

    仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值  的总和,为什么呢,因为要构成 a/b 然后不能约分  所以 gcd(a,b)==1,所以  分母 b的 欧拉函数值   ...

  8. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  9. BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)

    今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...

随机推荐

  1. sql - 选出指定范围的行

    Select no=Identity(int,1,1),* Into #temptable From dbo.tName order by fName --利用Identity函数生成记录序号 Sel ...

  2. [解答]对‘’未定义的引用 collect2: 错误: ld 返回 1

    写的makefile适用于32位,但是放到64位机器上make就有问题. 需要在makefile中gcc -o....的结尾加上-pthread,例如: bloomfilter.o: bloomfil ...

  3. CSV文件规则

    CSV文件规则 1 开头是不留空,以行为单位.2 可含或不含列名,含列名则居文件第一行.3 一行数据不跨行,无空行.4 以半角逗号(即,)作分隔符,列为空也要表达其存在.5 列内容如存在半角逗号(即, ...

  4. C++ 实现01背包动态规划

    简述一下01背包: 背包容量大小固定,有一些物品,每个物品都有重量和价值两个属性,且物品唯一不重复(即同一物品只能放入一个),放入物品的总重量不能超过背包容量 ,求放入背包的物品的总价值最大化.0代表 ...

  5. 配置wamp开发环境【2】 配置wamp开发环境之mysql的配置

    此前我已经将wamp配置的Apache.PHP.phpmyadmin全部配置完成,以上三种配置参照 配置wamp开发环境 下面我们来看看mysql的配置,这里用的是mysql5.5.20,下载地址: ...

  6. HDU 1114 Piggy-Bank(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目大意:根据储钱罐的重量,求出里面钱最少有多少.给定储钱罐的初始重量,装硬币后重量,和每个对应 ...

  7. 环状DNA序列

    大意: 一个DNA序列是环状的,这意味着有N个碱基的序列有N种表示方法(假设无重复).而这N个序列有一种最小的表示,这个最小表示的意思是这个序列的字典序最小(字典序的意思是在字典中的大小 比如ABC& ...

  8. wamp不能使用phpmyadmin,提示“You don't have permission to access /phpmyadmin/ on this server.” 转载

    换了win8之后wamp明显不怎么好用了,显示80端口被system占用,后是masql出现了403错误,多番百度谷歌找到了解决方案,这里与大家分享 当你安装完成wamp后,打开localhost或i ...

  9. requirejs 笔记

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. 浅谈购物车中cookie的使用

    购物车对于电商网站来说是一个非常重要的模块.最近自己的项目中也用到了,所以拿出来说说事! 购物车是用户选择商品的一个缓存的地方.其中包含了商品的基本信息,例如:商品的描述,商品的价格,商品的数量等等. ...