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. C#缓存处理

    第一种方式: 在ASP.NET中页面缓存的使用方法非常的简单,只需要在aspx页的顶部加这样一句声明即可: <%@ OutputCache Duration="60" Var ...

  2. 查看当前使用的shell

    1.实时查看当前进程中使用的shell种类:推荐 ps | grep $$ | awk '{print $4}' (注:$$表示shell的进程号) 2.最常用的查看shell的命令,但不能实时反映当 ...

  3. C#编程使用到的几种调试方式

    一.前言: 使用C#语言从08年算起,到现在也有6个年头的时间了. 但 是会使用调试进行辅助编程的时间,却只有5个年头,其中第一年里面,只能傻傻地敲着老师给的案例,不会写就一遍一遍重复手写编码,上机练 ...

  4. linux命令sed学习笔记

    sed其实就是两个主要的知识点,那就是“怎么选择”和“怎么操作”!

  5. JavaScript 快速入门回顾

    数据类型Number JavaScript不区分整数和浮点数,统一用Number表示,以下都是合法的Number类型: 123; // 整数123 0.456; // 浮点数0.456 1.2345e ...

  6. javascript 中的数据驱动页面模式

    前段时间一直在想前端MVC的意义.这个话题仁者见仁,但是MVC的使用方法给我提了一个管理数据的有意思的想法--数据管理和数据驱动页面.我们以前的思路一直是事件驱动页面,事件驱动页面合乎逻辑而且节约代码 ...

  7. 引入的iframe是跨域的, 如何控制其高度

    前提是你有编辑这个跨域的iframe的权限!! 1. main-domain.html (main display HTML) <!DOCTYPE html> <html> & ...

  8. PHPCMS标签大全

    {$head[title]} 页面标题,用法: {$phpcms[sitename]} 网站名称 用法: {$head[keywords]} 要害字 用法: {$head[description]} ...

  9. Python 关于正负无穷float(‘inf’)的一些用法

    Python中可以用如下方式表示正负无穷: float("inf"), float("-inf") 利用 inf 做简单加.乘算术运算仍会得到 inf > ...

  10. 使用awstats统计分析tengine日志访问量及各种http网站数据

    下载awstats最新安装包并解压 cd /usr/local/src wget http://www.awstats.org/files/awstats-7.3.tar.gz tar -zxvf a ...