Counting fractions

TimeLimit: 2000MS  MemoryLimit: 65536 Kb

Totalsubmit: 39   Accepted: 6  

Description

Consider the fraction, n/d, where n and d are positive integers. If <= d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d <= 8 in ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 21 elements in this set.

How many elements would be contained in the set of reduced proper fractions for d <= x?

Input

The input will consist of a series of x, 1 < x < 1000001.

Output

For each input integer x, you should output the number of elements contained in the set of reduced proper fractions for d <= x in one line.

Sample Input

3

8

Sample Output

3
21

Hint

HCF代表最大公约数

这里主要说一下素数筛法,该方法可以快速的选取出1~N数字中的所有素数。时间复杂度远小于O(N*sqrt(N))
方法为:从2开始,往后所有素数的倍数都不是素数。最后剩下的数都是素数。
再说说欧拉公式,用来解决所有小于n中的数字有多少个与n互质,用Ψ(n)表示。
Ψ(n)=n*(1-1/q1)*(1-1/q2)*……*(1-1/qk),n为和数,其中qi为n的质因数。
Ψ(n)=n-1,n为质数
注意:关于数论的题很容易超过int类型的范围,多考虑用long long类型。
欧拉函数请参阅:http://zh.wikipedia.org/zh-cn/%E6%AC%A7%E6%8B%89%E5%87%BD%E6%95%B0

代码

 

posted on 2013-04-30 21:26 小鼠标 阅读(1278) 评论(0)  编辑 收藏 引用 所属分类: 数论

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2013年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

随笔分类(111)

随笔档案(127)

friends

最新评论

  • 1. re: 线段树
  • 是这个样子的,所以在OJ有时候“卡住”了也不要太灰心,没准真的不是自己的原因呢。
    加油,祝你好运啦!
  • --小鼠标
  • 2. re: 线段树
  • 对于编程竞赛来说,Java所需时间一般为C/C++的两倍。合理的竞赛给Java的时间限制是给C/C++的两倍。
  • --伤心的笔
  • 3. re: poj1273--网络流
  • 过来看看你。
  • --achiberx
  • 4. re: (转)ubuntu11.10无法启动无线网络的解决方法
  • 膜拜大神。。查了一个下午资料终于在这里解决了问题。。神牛说的区域赛难道是ACM区域赛。。?
  • --Hang
  • 5. re: 快速排序、线性时间选择
  • 博主,谢谢你的文章。你的方法可以很好的处理分区基准在数组中重复的情况,书上的方法遇到这种输入会堆栈溢出。书上给出了解释但给的方法貌似不简洁。
  • --lsxqw2004

阅读排行榜