题目大意:Euler's Totient的应用。
几乎和UVa 10179 - Irreducable Basic Fractions一样,于是偷了个懒,直接用10179题的代码,结果WA了,感觉一样啊...然后就搜,看到n=1的特殊情况,好吧,读题的时候也小小注意了一下"less than" 呢,写代码时就忘的一干二净了...读题时应该把注意事项记下来的...
1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 #include <bitset> 5 using namespace std; 6 typedef vector<int> vi; 7 typedef long long ll; 8 #define MAXN 10000000 9 10 bitset<MAXN+100> bs; 11 vi primes; 12 13 void sieve(ll upper) 14 { 15 bs.set(); 16 bs.set(0, false); 17 bs.set(1, false); 18 for (ll i = 2; i <= upper; i++) 19 { 20 if (bs.test((size_t)i)) 21 for (ll j = i*i; j <= upper; j++) 22 bs.set((size_t)j, false); 23 primes.push_back((int)i); 24 } 25 } 26 27 vi primeFactors(ll n) 28 { 29 vi factors; 30 int idx = 0, pf = primes[idx]; 31 while (n != 1 && (pf*pf <= n)) 32 { 33 while (n % pf == 0) 34 { 35 n /= pf; 36 factors.push_back(pf); 37 } 38 pf = primes[++idx]; 39 } 40 if (n != 1) factors.push_back(n); 41 return factors; 42 } 43 44 int main() 45 { 46 #ifdef LOCAL 47 freopen("in", "r", stdin); 48 #endif 49 sieve(MAXN); 50 int n; 51 while (scanf("%d", &n) && n) 52 { 53 if (n == 1) 54 { 55 printf("0\n"); 56 continue; 57 } 58 vi factors = primeFactors(n); 59 vi::iterator last = unique(factors.begin(), factors.end()); 60 int result = n; 61 for (vi::iterator it = factors.begin(); it != last; it++) 62 result = result - result/(*it); 63 printf("%d\n", result); 64 } 65 return 0; 66 }