题目链接: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. 在IE中检查控件是否安装成功

    步骤: 1.打开图片上传页面   2.打开IE加载项   3.在加载项中可以看到加载的控件 4.点击详细信息,查看文件名称和文件位置

  2. 跨域Ajax请求(jQuery JSONP MVC)

    通过jQuery的$.ajax方法发送JSONP请求 js代码 <script type="text/javascript"> function jsonptest2( ...

  3. Hackfive 使用TextSwitcher和ImageSwitcher实现平滑过渡

    1. 应用场景: 通过向左和向右的导航按钮浏览日期列表 在日期选择空间中改变日期 倒计时始终 新闻刚要 2.用到的知识点是:     TextSwitcher和ImageSwitcher     Te ...

  4. 11款PHP集成开发环境

    1. Zend Studio Zend Studio 来自 Zend —— PHP 幕后的精英团队. 它真的是很不错.它运行于三大系统:Windows.Mac OS X 和 Linux.而且绝对能够提 ...

  5. Unity本地持久化类Playerprefs使用详解

    一.PlayerPrefs是什么? PlayerPrefs是Unity3d提供了一个用于数据本地持久化保存与读取的类.工作原理十分简单,就是以key-value的形式将数据保存在本地,然后在代码中可以 ...

  6. javascript 视频播放指定的时间段

    javascript 视频播放指定的时间段 一.html5 vedio: //指定开始时间 player.currentTime=startPoint; player.play(); //使用事件来控 ...

  7. Atcoder Grand Contest 031C(构造,思维,异或,DFS)

    #include<bits/stdc++.h>using namespace std;int n,a,b,sum;void dfs(int x,int y,int ban){    if( ...

  8. 接口测试简单介绍、及jmeter的简单使用

    1.接口测试简单介绍 接口测试其实就是功能测试,是从数据库查询到数据,返回查询结果 接口返回的数据都是json,json是一种通用的数据类型. 接口测试的优点:能在稍微偏底层的地方发现bug,越底层发 ...

  9. Java面向对象之关键字static 入门实例

    一.基础概念 静态关键字 static 是成员修饰符,直接用于修饰成员. (一)特点: 1.被静态修饰的成果,可以直接被类名所调用. 2.静态成员优先于对象存在. 3.静态成员随着类的加载而加载.随着 ...

  10. delphi 给字符指针分配内存

    今天,对接第三方dll的时候出现如下问题: 接口声明如下: long BL_tradeBalance (char *MerchantNumber,char *PosId,char *OperatorN ...