UVa 10299 - Relatives

时间:2023-01-06 20:19:54

  题目大意:Euler's Totient的应用。

  几乎和UVa 10179 - Irreducable Basic Fractions一样,于是偷了个懒,直接用10179题的代码,结果WA了,感觉一样啊...然后就搜,看到n=1的特殊情况,好吧,读题的时候也小小注意了一下"less than" 呢,写代码时就忘的一干二净了...读题时应该把注意事项记下来的...

 #include <cstdio>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
#define MAXN 10000000 bitset<MAXN+> bs;
vi primes; void sieve(ll upper)
{
bs.set();
bs.set(, false);
bs.set(, false);
for (ll i = ; i <= upper; i++)
{
if (bs.test((size_t)i))
for (ll j = i*i; j <= upper; j++)
bs.set((size_t)j, false);
primes.push_back((int)i);
}
} vi primeFactors(ll n)
{
vi factors;
int idx = , pf = primes[idx];
while (n != && (pf*pf <= n))
{
while (n % pf == )
{
n /= pf;
factors.push_back(pf);
}
pf = primes[++idx];
}
if (n != ) factors.push_back(n);
return factors;
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
sieve(MAXN);
int n;
while (scanf("%d", &n) && n)
{
if (n == )
{
printf("0\n");
continue;
}
vi factors = primeFactors(n);
vi::iterator last = unique(factors.begin(), factors.end());
int result = n;
for (vi::iterator it = factors.begin(); it != last; it++)
result = result - result/(*it);
printf("%d\n", result);
}
return ;
}