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