U - Relatives(欧拉函数)
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
解题思路:欧拉函数:求不大于n且与n互质的数的个数。

AC代码:
#include<bits/stdc++.h>
using namespace std;
int Euler(int x){
int r=x;
for(int i=;i*i<=x;i++){//由于任何一个合数都至少有一个不大于根号x的质因子,所以只需遍历到根号x即可
if(x%i==){
r=r/i*(i-);//欧拉函数的通式,先除后乘,避免数据溢出
while(x%i==)x/=i;//消除x中所有质因子i,直到不能被i整除
}
}
if(x>)r=r/x*(x-);//如果x大于1,说明还有一个质因子没有除掉
return r;//返回个数
}
int main(){
int n;
while(cin>>n&&n)
cout<<Euler(n)<<endl;
return ;
}
U - Relatives(欧拉函数)的更多相关文章
- poj2407 Relatives 欧拉函数基本应用
题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...
- POJ 2407 Relatives(欧拉函数)
题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...
- POJ2407–Relatives(欧拉函数)
题目大意 给定一个正整数n,要求你求出所有小于n的正整数当中与n互质的数的个数 题解 欧拉函数模板题~~~因为n过大~~~所以直接用公式求 代码: #include<iostream> # ...
- POJ 2407 Relatives 欧拉函数题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 数论 - 欧拉函数模板题 --- poj 2407 : Relatives
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11372 Accepted: 5544 Descri ...
- POJ 2407 Relatives(欧拉函数入门题)
Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...
- POJ 2407:Relatives(欧拉函数模板)
Relatives AC代码 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16186 Accept ...
- POJ 2407 Relatives 【欧拉函数】
裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i ...
- POJ2407 Relatives(欧拉函数)
题目问有多少个小于n的正整数与n互质. 这个可以用容斥原理来解HDU4135.事实上这道题就是求欧拉函数$φ(n)$. $$φ(n)=n(1-1/p_1)(1-1/p_2)\dots(1-1/p_m) ...
- 欧拉函数K - Relatives
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n). 特殊性质:当n为奇数时,φ(2n)=φ(n), φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..( ...
随机推荐
- Delphi:Indy9的IdFTP完全使用
Delphi 7自带的INDY控件,其中包含了IdFTP,可以方便的实现FTP客户端程序,参考自带的例子,其中有上传.下载.删除文件,但是不包含对文件夹的操作,得自己实现上传.下载.删除整个文件夹(带 ...
- JDBC的存储过程
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/stored-procedure.html: 正如一个Connection对象创建了Statement和 ...
- JSTL-函数标签库
主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 一.fn:contains() fn:contains()函数决定了一个输入字符串是否包含 ...
- 【python自制】让大白成为你的个人助手!
我做这个软件就是要让卖萌进行究竟! 官方站点:http://www.jackeriss.com/companions.htm GitHub:https://github.com/Jackeriss/C ...
- 介绍css 的3D 变换(3D transform)
https://desandro.github.io/3dtransforms/docs/card-flip.html ---------------------------------------- ...
- android/java经常使用的工具类源代码
anroid.java经常使用的工具类源代码,当中包含文件操作.MD5算法.文件操作.字符串操作.调试信息log.base64等等. 下载地址:http://download.csdn.net/det ...
- 公用表表达式(CTE)
公用表表达式(CTE,Common table expression)是和派生表很相似的另一种形式的表表达式,而且具有一些重要优势.CTE 是在 SQL Server 2005 中引入的,是ANSI ...
- 工作总结 default Console.WriteLine(default(Guid));
泛型代码中的默认关键字 在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T: T 是引用类型还是值类型. 如果 T 为值类型,则它是数值还是结构. 给定参数化 ...
- redis集群状态信息维护脚本
近期在做redis相关的东西.须要把2台redis切分成16个shard. 16个主shard分布在4台128G的机器上,从shard分布在12台64G的机器上.因为机器太多,查询相关的信息不太方便. ...
- 图像处理之基础---滤波器之高斯低通滤波器的高斯模板生成c实现
()代码实现 对原图进行高斯平滑,去除图像中的计算噪声void Bmp::MakeGauss(double sigma,double **pdKernel,int *pnWindowSize){ // ...