一、题目

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,让你去用1~N的数字去组合成Farey序列。关于Farey序列,依题意可知,就是1~N的数字中互素的a,b,其中a<b,就可以凑成一个a/b。然后问有多少个不同的a/b。

我们先看2,就一个,看3,发现2有的3肯定有,然后其余的就是与3互质的数与3凑成的a/b。再看4,3有的还是有,然后再加上与4互质的数与4凑成的a/b。依次递推下去。就是欧拉函数的前N项和。用一个数组累加保存下来,就是所有结果了,再用线性筛法去求欧拉函数(可以看我之前的欧拉函数学习笔记),速度绝对够。需要注意的是,结果增长的很快,需要用long long。

三、AC代码

  

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1e6+5;
int Phi[MAXN], Prime[MAXN], nPrime;
long long Ans[MAXN]; void Euler()
{
memset(Phi, 0, sizeof(Phi));
Phi[1] = 1;
nPrime = 0;
for(int i = 2; i < MAXN; i++)
{
if(!Phi[i]) //i为素数
{
Phi[i] = i - 1;
Prime[nPrime++] = i;
}
for(int j = 0; j < nPrime && (long long)i*Prime[j] < MAXN; j++)
{
if(i%Prime[j])
{ Phi[ i*Prime[j] ] = Phi[i]*(Prime[j]-1);
}
else
{
Phi[ i*Prime[j] ] = Phi[i]*Prime[j];
break;
}
}
}
return;
} void solve()
{
Euler();
Ans[2] = Phi[2];
for(int i = 3; i < MAXN; i++)
{
Ans[i] = Ans[i-1] + Phi[i];
}
return;
} int main()
{
int N;
solve();
while(cin>>N && N)
{
cout << Ans[N] << endl;
}
return 0;
}

  

最新文章

  1. Linux 内核中的 Device Mapper 机制
  2. vim_cfg
  3. oracle更新语句merge和update
  4. 快速入门系列--WCF--07传输安全、授权与审核
  5. Java实验四和实验五
  6. SBT 构建scala eclipse开发
  7. apue第四章学习总结
  8. -webkit-filter属性用来干什么
  9. linux下从源代码安装git
  10. Linux逻辑卷创建
  11. json数据与字符串相互转化的例子
  12. openstack api
  13. css position 定位
  14. C#中的static静态变量的用法
  15. poj1830
  16. Mybatis学习(8)逆向工程
  17. 最全Pycharm教程(32)——依据FHS在Linux上安装Pycharm
  18. 使用PL/SQL能查询oracle中数据,在for update 语句中一直卡住
  19. &lt;a&gt;标签的特殊和文本的样式
  20. PHP按符号截取字符串的指定部分

热门文章

  1. 788. Rotated Digits 旋转数字
  2. 1-new对象与直接构建对象
  3. 1.SQL
  4. select样式调整
  5. rest 参数和扩展运算符
  6. ios7适配--隐藏status bar
  7. Redis主从服务部署
  8. (一)ASP.NET中JavaScript的中英文(多语言)实现方案
  9. JAVA读取控制台的输入【转】
  10. java的多线程安全,ReentrantLock与synchronized锁