A - Farey Sequence

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 <= 10 6). 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

题意:求欧拉函数的前n项和。

题解:打表筛选即可。

#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
ll a[maxn];
void get()
{
for(int i=;i<maxn;i++)
{
if(!a[i])
{
for(int j=i;j<maxn;j+=i) //已经包含了素筛的思想
{
if(!a[j])
a[j]=j;
a[j]=a[j]/i*(i-);
}
}
a[i]+=a[i-];
}
}
int main()
{
get();
int n;
while(cin>>n&&n)
cout<<a[n]<<endl;
return ;
}

POJ 2478 Farey Sequence(欧拉函数前n项和)的更多相关文章

  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 —— 欧拉函数

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

  5. poj2478 Farey Sequence (欧拉函数)

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

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

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

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

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

  8. 51Nod 1239 欧拉函数前n项和 杜教筛

    http://www.51nod.com/Challenge/Problem.html#!#problemId=1239 AC代码 #include <bits/stdc++.h> #de ...

  9. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

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

随机推荐

  1. 【poj1182】 食物链

    http://poj.org/problem?id=1182 (题目链接) 题意 中文题 Solution 带权并查集. 神犇博客,秒懂 fa记录父亲,r记录与父亲的关系.%3运用的很巧妙. 代码 / ...

  2. IOS开发 证书总结

    开发者证书   ------>>  首先你必须获得apple开发者证书,上图这个文件就是apple开发者证书,只要有apple的开发者账号就可以下载到,此证书可以直接到 developer ...

  3. Linux文件权限;ACL;Setuid、Setgid、Stick bit特殊权限;sudo提权

    相关学习资料 http://blog.sina.com.cn/s/blog_4e2e6d6a0100g47o.html http://blog.csdn.net/aegoose/article/det ...

  4. java将一维数组拆分成二维数组

    package staticFactory; public class Array { public static void main(String[] args) { String[] a=new ...

  5. 解决IE apk变成zip:Android 手机应用程序文件下载服务器 配置解决方法

    APK文件其实是zip格式,但后缀名被修改为apk,通过UnZip解压后,可以看到Dex文件,Dex是Dalvik VM executes的全称,即Android Dalvik执行程序,并非Java ...

  6. thinkphp空控制器的处理

    <?php namespace Admin\Controller; use Think\Controller; class DengLuController extends Controller ...

  7. python之BIF函数在列表中的应用

    1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 2 T ...

  8. jQuery特效

    基础特效 方法 描述 hide() 立即隐藏jQuery对象内的所有元素 hide(time).hide(time, easing) 在指定的时间内以动画方式隐藏jQuery对象内的所有元素,并可选一 ...

  9. git常见问题解决办法

    1,git status乱码 git config --global core.quotepath false 执行完后再使用时,就显示正常了

  10. Sqlserver日期函数应用

    1.获取当前时间 SELECT  GETDATE() AS '当前日期' ,         DATENAME(year, GETDATE()) AS '年' ,         DATENAME(m ...