Relatives
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 11422
Accepted: 5571

Description









Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.

Input









There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

Output









For each test case there should be single line of output answering the question posed above.

Sample Input









7

12

0

Sample Output









6

4

Source









Waterloo local 2002.07.01

题目大意:给你一个正整数N。求在小于N的范围内。有多少个正整数与N互质?

思路:典型的欧拉phi函数

欧拉函数(摘自百度百科):

在数论,对正整数n,欧拉函数φ(n)是少于或等于n的数中与n互质的数的数目。

φ(n) = n(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),当中p1, p2……pn为x的所

有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数(小于等于1)就是1本身)。

(注意:每种质因数仅仅一个。比方12=2*2*3那么φ(12)=12*(1-1/2)*(1-1/3)=4

一些定理:

1>若n是质数p的k次幂。φ(n) = p^k-p^(k-1) =(p-1)p^(k-1),由于除了p的倍数外,

其它数都跟n互质。

2>若n为素数,φ(n) 
= n - 1;(同第6条)

3>当n为奇数时,φ(2n)=φ(n)。

4>欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

5>欧拉定理:对不论什么两个互质的正整数a, m, m>=2有 a^φ(m) ≡ 1(mod m)。

6>费马小定理:当m是质数p时,此式则为:a^(p-1)≡1(mod m)。

#include <stdio.h>
#include <math.h> int Euler(int n)
{
int i,ret = n;
for(i = 2; i <= sqrt(1.0*n); i++)
{
if(n % i == 0)
{
ret = ret - ret/i;
}
while(n % i == 0)
n /= i;
}
if(n > 1)
ret = ret - ret/n;
return ret;
}
int main()
{
int p;
while(~scanf("%d",&p) && p)
printf("%d\n",Euler(p));
return 0;
}

最新文章

  1. ADT for Eclipse无法升级到23.0的解决方法(确保您的网络能够访问google的地址)
  2. VS 工程的 输出路径和工作路径的区别
  3. 新增资产时YTD折旧与累计折旧录入错误如何处理
  4. Windows Server 2012 R2 里面如何安装Net Framework 3.5
  5. 进军swift
  6. 人工免疫算法-python实现
  7. javascript的词法分析
  8. UIView UITableView 背景图片添加
  9. Looksery Cup 2015 A. Face Detection 水题
  10. C#导出DataGridView到Excel
  11. js实现堆排序
  12. java集合经常出现空指针问题的解决方案
  13. asp.net UpdatePanel 不能局部刷新问题汇总
  14. for循环、穷举法和迭代
  15. codeforces 492E. Vanya and Field(exgcd求逆元)
  16. day2_python的数据类型,sys,os模块,编码解码,列表,字典
  17. 微信小程序小结02-- 完整的demo
  18. LODOP获取打印机状态码和状态码含义测试
  19. Redis之Transactions(事物)
  20. DEMO6:坐标添加文字标签的JavaScript插件

热门文章

  1. HTTP Header中Accept-Encoding
  2. Linux signal 那些事儿(4)信号的deliver顺序【转】
  3. The C programming language [book]
  4. C#图片转成流,流转成图片,字节转图片,图片转字节的方法
  5. C++面向对象程序设计_Part1
  6. 你知道如何在springboot中使用redis吗
  7. React 入门之路
  8. ios notification
  9. 关于文本处理sort-cut-wc详解
  10. POJ-1190-生日蛋糕-DFS(深搜)-枚举-多重剪枝