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剛開始我傻X了,本著蒟蒻的思維,照著歐拉函數的公式寫了如下的代碼,先篩素數然後分解質因數最後再算公式
//歐拉函數h(n)=n*(1-1/p1)*(1-1/p2)*'''''*(1-1/pk); #include#include #include #include #define MAXN 1010 using namespace std; bool isPrime[MAXN]; int Prime[MAXN],PriNum[MAXN],cnt=0,tot=0; void GetPrime() { cnt=0; for(int i=1;i >N; if(!N) break; DecQualityFactor(N); cout< 好吧這個代碼根本沒辦法過,因為題目給的n的范圍太大了,數組完爆內存,也就是說這題根本就不能用數組保存什麼質因數和素數的。
下面才是真正的AC代碼,又短又快神神哒
//歐拉函數h(n)=n*(1-1/p1)*(1-1/p2)*'''''*(1-1/pk); #include#include #include #include #define MAXN 1000 using namespace std; int h(int n) { int ans=n; for(int i=2;i<=n;i++) { if(n%i==0) { ans=ans/i*(i-1); n/=i; while(n%i==0) n/=i; } } return ans; } int main() { while(1) { int N; cin>>N; if(!N) break; cout<