Relatives
2016-03-12 09:52
225 查看
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
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long LL ;
int euler(int n){ //返回euler(n)
int res=n,a=n;
for(int i=2;i*i<=a;i++){
if(a%i==0){
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
int main (){
int x;
while(~scanf("%d",&x)&&x){
printf("%d\n",euler(x));
}
return 0;
}
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long LL ;
int euler(int n){ //返回euler(n)
int res=n,a=n;
for(int i=2;i*i<=a;i++){
if(a%i==0){
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
int main (){
int x;
while(~scanf("%d",&x)&&x){
printf("%d\n",euler(x));
}
return 0;
}
相关文章推荐
- 《调查表与调查结果分析》
- mysql-5.7.11-winx64中mysql.exe双击无法看到进程,CMD下3534错误,无法启动的解决方法
- mysql-5.7.11-winx64中mysql.exe双击无法看到进程,CMD下3534错误,无法启动的解决方法
- java SE基础整理(面向对象)
- 【WIN32之旅】给托盘图标加上MOUSE_ENTER、MOUSE_HOVER、MOUSE_LEAVE消息
- nyoj32---组合数
- python async/await
- C++设计模式编程中的迭代器模式应用解析
- Window On Top–让任意窗口位于顶层
- Mac文件系统
- android studio更新到1.5后遇到的问题
- 前后端分离开发部署模式
- 和GCD相关的“个数”及“求和”问题——hdu 2588、nyist 1007
- python asyncio
- QML中的BusyIndicator
- Eclipse在启动模拟器时出错
- Html5 手机wap开发、微信开发、APP开发的优缺点-创业者必知
- 漫谈架构2
- 网络爬虫个人博客
- CentOS 6.4下安装和配置Samba 第2页_服务器应用_Linux公社-Linux系统门户网站