题目链接:http://poj.org/problem?id=2478

Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19736   Accepted: 7962

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
 
题目大意:很容易可以发现是求2-n的所有数的欧拉函数值之和
看代码:
/**
有三条特性
若a为质数 phi[a]=a-1
若a为质数,b%a==0 phi[a*b]=phi[b]*a;
若a b 互质 phi[a*b]=phi[a]*phi[b](当a为质数 如果b%a!=0) */
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
const int maxn=1e6+;
int phi[maxn],prime[maxn],p[maxn];//phi[i]代表i的欧拉函数值 prime[i]=0代表是素数 1代表不是素数 p存储素数
void make()
{
phi[]=;//特例
int num=;
for(int i=;i<=maxn;i++)
{
if(!prime[i])//是素数
{
p[num++]=i;//
phi[i]=i-;//素数的欧拉函数值就是它的值减1
}
for(int j=;j<num&&p[j]*i<maxn;j++)//用当前已经得到的素数筛去p[j]*i
{
prime[p[j]*i]=;//可以确定p[j]*i不是质数
if(i%p[j]==)//第二条特性
{
phi[p[j]*i]=phi[i]*p[j];
break;//欧拉筛的核心语句 保证每个数只会被自己最小的质因子筛掉一次
}
else phi[p[j]*i]=phi[i]*phi[p[j]];
}
}
return ;
} int main()
{
make();
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
LL sum=;
for(int i=;i<=n;i++) sum+=phi[i];
printf("%lld\n",sum);
}
// for(int i=1;i<=100;i++) cout<<phi[i]<<" "; return ;
}

Farey Sequence(欧拉函数板子题)的更多相关文章

  1. poj2478 Farey Sequence (欧拉函数)

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

  2. POJ2478 Farey Sequence —— 欧拉函数

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

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

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

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

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

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

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

  6. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

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

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

  8. poj2407(欧拉函数模板题)

    题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...

  9. UVA 10820 欧拉函数模板题

    这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...

随机推荐

  1. K860i的109升级需要的PinyinIME.apk和QuickSearchBox.apk两个文件

    联想K860i的109版本终于可以正常更新了,感谢下乐Phone之家的蚊子咬(http://bbs.lephone.cc/space-uid-6410.html)提供了关键的两个文件:PinyinIM ...

  2. Instruments Tutorial for iOS: How To Debug Memory Leaks【转】

    If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for vis ...

  3. Thumbnail 图片帮助

    public class Thumbnail { private Image srcImage; private string srcFileName; /// <summary> /// ...

  4. 最近的一些零碎知识点,jquery遍历

    1.使按钮无法点击 $(“#btn”).attr("disable",true); 2.返回上一个页面 history.back(-1); 3.$(this).siblings() ...

  5. windows下vscode 搭建python开发环境

    1.vscode https://code.visualstudio.com/ 下载 2.python下载 https://www.python.org/downloads/windows/  exe ...

  6. iOS App 内部跳转(设置、Wifi、蓝牙...)关键词

    1.iOS 10 以前: 蜂窝网络:prefs:root=MOBILE_DATA_SETTINGS_ID Wi-Fi:prefs:root=WIFI 定位服务:prefs:root=LOCATION_ ...

  7. 用python写个快排

    快排过程比较简单就直接上代码了: #!/usr/bin/python3 def quik_sort(L, left, right): if left <= right: key = L[left ...

  8. jq学习笔记(二)

    jq笔记-dom篇-慕课网学习笔记 1.jQuery节点创建与属性的处理 创建元素节点: 1.$("<div></div>") 创建为本节点: 1.$(&q ...

  9. selenium定位元素提示‘元素不可见’问题解决方法

    最近在使用selenium的过程中发现有元素能够在页面中查找到,但是pycharm中运行时始终报错element not visible,于是使用如下方法成功解决问题. 1.driver.find_e ...

  10. 条目六《当心C++编译器中最烦人的分析机制》

    当心C++编译器中最烦人的分析机制 C++是较为底层的面相对象语言,在底层的语法规则分析中,有很多隐藏的分析机制. C++中的普遍规律相符,即尽可能地解释为函数声明. 把形式参数的声明用括号括起来是非 ...