题目链接: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. thedao

    TheDao 简化版解释 the Dao 合约 contract f1{ function transfer() { if (acccount[m]>=100) { m.send(100) ac ...

  2. 「POJ 2182」 Lost Cows

    题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...

  3. virtueBox实现虚拟机的复制和粘贴

    1.在设备--共享粘贴板--勾选双向. 2.在设备--拖放--勾选双向. 3.在设备--安装增强功能,然后进入虚拟机安装增强功能即可.

  4. 20165219 2017-2018-2《Java程序设计》课程总结

    20165219 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 20165219 我期望的师生关系 20165219学习基础与C语言基础调查 20165219 ...

  5. js 封装常用方法

    1. 获取数据类型 function getType(params) { , -) } 2. 深拷贝 function deepCopy(params) { var obj; if (typeof p ...

  6. HDP 中 yarn 和 MR2 的配置

    以下说明均以集群中 slave 结点的配置为 48G内存,12块硬盘,12核(core) CPU 为例. 在 Yarn 中,一个 Container 是一个基础的包含内存和CPU 的单元.为了较好的平 ...

  7. CentOS 6.3下Samba服务器的安装与配置【转载】

    本文转载自 园友David_Tang的博客,如有侵权请联系本人及时删除,原文地址: http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.h ...

  8. luoguP1401 城市

    https://www.luogu.org/problemnew/show/P1401 二分答案网络流判断是否可行即可 #include <bits/stdc++.h> using nam ...

  9. [Swift实际操作]九、完整实例-(5)创建BaseViewController作为控制器的基类

    本文将给项目中的所有视图控制器,创建一份基类.该基类用来定义一些共用的属性和方法. 首先在用来放置视图控制器类的文件夹上点击鼠标右键,打开右键 菜单. 选择[New File]创建文件选项. 在弹出的 ...

  10. 【Guava】RateLimiter类

    Guava官方文档-RateLimiter类 原文链接 作者:Dimitris Andreou  译者:魏嘉鹏 校对:方腾飞 RateLimiter 从概念上来讲,速率限制器会在可配置的速率下分配许可 ...