poj 2407 Relatives
2016-03-10 20:23
246 查看
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 questiosed above.
Sample Input
Sample Output
解题思路:
利用公式φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)
其中p1, p2……pn为x的所有质因数,φ(x)为x里所有质因数的个数。
代码如下:
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 questiosed above.
Sample Input
7 12 0
Sample Output
6 4
解题思路:
利用公式φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)
其中p1, p2……pn为x的所有质因数,φ(x)为x里所有质因数的个数。
代码如下:
#include<iostream> using namespace std; int main() { int n,sum; while(cin>>n&&n!=0) { sum=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { sum=sum-sum/i; n/=i; while(n%i==0) n/=i; } } if(n>1) sum=sum-sum/n; cout<<sum<<endl; } return 0; }
相关文章推荐
- 第2周项目3 小试循环
- 单例模式
- LeetCode 8. String to Integer (atoi)
- Leetcode 160 Intersection of Two Linked Lists 单向链表
- 一个return引发的血案
- Python基础——module
- 客户端——解析json数据
- BigInteger类的一些用法
- android开发之java内存泄露分析
- 从angularJS改道Vue.js,趟过第一个坑!
- JS里的onclick事件
- Pg188-3 构造方法
- BZOJ 1264: [AHOI2006]基因匹配Match 树状数组+DP
- 携程Android App插件化和动态加载实践
- Nginx为什么比Apache Httpd高效:原理篇
- content-type 类型讲解
- Apache commons-io
- 跳青蛙问题与变态跳青蛙问题
- 1042/72 n!
- 数据预处理