Relatives+水题+欧拉函数+素数打表的基本应用+poj
2014-07-26 18:18
316 查看
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
Sample Output
解决方案:水题,没什么好说的。
code:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11122 | Accepted: 5383 |
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
解决方案:水题,没什么好说的。
code:
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #define MMAX 100000 using namespace std; vector<int >prime; bool vis[MMAX]; void init_prime(){ memset(vis,false,sizeof(vis)); prime.clear(); prime.push_back(2); for(int i=3;i<MMAX;i+=2){ if(!vis[i]){ prime.push_back(i); for(int j=i+i;j<MMAX;j+=i) vis[j]=true; } } } int euler(int x){ long long res=x; int len=prime.size(); for(int i=0;i<len;i++){ if(x%prime[i]==0){ res=res/prime[i]*(prime[i]-1); while(x%prime[i]==0) x/=prime[i]; } } if(x>1) res=res/x*(x-1); return res; } int main() { long long n; init_prime(); while(~scanf("%lld",&n)&&n){ printf("%d\n",euler(n)); } return 0; }
相关文章推荐
- SGU 102 Coprimes ---- 欧拉函数、素数的应用&&GCD水题
- POJ 2909 Goldbach's Conjecture(我的水题之路——任一数为素数对之和)
- POJ 2773 Happy 2006 欧拉函数的应用
- (Relax 数论1.13)POJ 2909 Goldbach's Conjecture(欧拉筛法的应用:给出一个偶数,要求在这个偶数的范围内,有几对素数和是等于这个偶数的,注意素数对的不重复性! )
- (Relax 1.15)POJ 2773 Happy 2006(欧拉函数的应用:求与n互质的第k个数)
- poj 2478【线性筛素数+欧拉函数】
- (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
- (顺序表应用5.1.1)POJ 3750 小孩报数问题(基本的约瑟夫环问题:给出人数n,出发位置w,间隔数s)
- (顺序表应用5.1.1)POJ 3750 小孩报数问题(基本的约瑟夫环问题:给出人数n,出发位置w,间隔数s)
- (Relax 水题1.5)POJ 1183 反正切函数的应用
- poj 1258 并查集的基本应用 最小生成树
- poj 2352 树状数组 基本应用
- poj 3264 RMQ 基本应用
- nefu119 组合素数 算术基本定理的应用
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(素数筛的简单应用)
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(我的水题之路——加i个d后的第几个素数)
- POJ 2828 Buy Tickets(线段树的基本应用)
- poj 2480 (欧拉函数应用)
- poj2407 Relatives 欧拉函数基本应用
- (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))