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;
}
POJ2407_Relatives【欧拉phi函数】【基本】的更多相关文章
- HDU 1286 找新朋友 (欧拉phi函数打表)
题意:你懂得. 析:一看这个题应该是欧拉phi函数,也就说欧拉phi函数是指求从 1 到 n 中与 n 互素的数的个数,这个题很明显是这个意思嘛,不多说了. 代码如下: #include <io ...
- 积性函数初步(欧拉$\varphi$函数)
updata on 2020.4.3 添加了欧拉\(\varphi\)函数为积性函数的证明和它的计算方式 1.积性函数 设\(f(n)\)为定义在正整数上的函数,若\(f(1)=1\),且对于任意正整 ...
- 容斥原理、欧拉函数、phi
容斥原理: 直接摘用百度词条: 也可表示为 设S为有限集, ,则 两个集合的容斥关系公式:A∪B = A+B - A∩B (∩:重合的部分) 三个集合的容斥关系公式:A∪B∪C = A+B+C - A ...
- hdu 5279 Reflect phi 欧拉函数
Reflect Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_chi ...
- (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
/* * POJ_2407.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...
- 求一个极大数的欧拉函数 phi(i)
思路: 因为当n>=1e10的时候,线性筛就不好使啦.所以要用一个公式 φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) 证明详见:<公式 ...
- POJ3090 巧用欧拉函数 phi(x)
POJ3090 给定一个坐标系范围 求不同的整数方向个数 分析: 除了三个特殊方向(y轴方向 x轴方向 (1,1)方向)其他方向的最小向量表示(x,y)必然互质 所以对欧拉函数前N项求和 乘2(关于( ...
- (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)
题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...
随机推荐
- Selenium 2.0自动化测试
http://blog.sina.com.cn/s/blog_b6142fb401017oo6.html http://www.cnblogs.com/halia/p/3562132.html?utm ...
- Codeforces Gym10081 A.Arcade Game-康托展开、全排列、组合数变成递推的思想
最近做到好多概率,组合数,全排列的题目,本咸鱼不会啊,我概率论都挂科了... 这个题学到了一个康托展开,有点用,瞎写一下... 康托展开: 适用对象:没有重复元素的全排列. 把一个整数X展开成如下形式 ...
- python对象的复制问题
list 的拷贝问题: 1, >>> a [1, 2] >>> b=a[:] >>> b [1, 2] >>> b[0]=20 ...
- 参数化2--CSV Data Set Config 参数化配置
众所周知,在进行接口测试的过程中,需要创建不同的场景(不同条件的输入,来验证不同的入参的返回结果).因而,在日常的自动化接口监控或商品监控等线上监控过程中,需要配置大量的入参来监控接口的返回是否正确. ...
- Excel Sheet Column Title - LeetCode
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- Codeforces 734 F Anton and School
Discription Anton goes to school, his favorite lessons are arraystudying. He usually solves all the ...
- zabbix-agent安装报错
最近接触了zabbix,觉得挺好用的,再一次安装agent的过程中,报了如下错误: [root@11005499 ~]# yum install zabbix-agent -y ... groupad ...
- 查找python项目依赖并生成requirements.txt
1.如果使用virtualenv环境,直接使用 pip freeze > requirements.txt ➜ ~ .virtualenvs/xxx/bin/pip freeze > r ...
- SpringMVC项目中web.xml中的节点载入顺序问题
SpringMVC项目中web.xml中的节点载入顺序问题,之前以为web.xml中就是一些配置信息,和节点的顺序没有关系.后来才发现初始化时的载入顺序是和节点的顺序相关的. 完整的web.xml文件 ...
- 微信小程序-使用腾讯Wxpage
微信小程序想要更快的速度吗? 满足你 https://github.com/tvfe/wxpage#-c%E5%AE%9A%E4%B9%89 使用超简单(导入wxpage.js,最后使用对象名:P): ...