We are asked: "For given integer n, count the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n." This is Euler's totient function φ(n). Input: n (1 ≤ n ≤ 1e9). Output: φ(n). We need to compute φ(n) efficiently for n up to 1e9. That can be done by prime factorization. Since n is up to 1e9, we can factor by trial division up to sqrt(n) which is up to 31623. That's fine. So algorithm: if n == 1, φ(1)=1 (since 1 is relatively prime to 1? Usually φ(1)=1 (the number of integers ≤1 that are coprime to 1 is 1). Check: positive integers ≤ 1 that are coprime to 1: only 1, because gcd(1,1)=1. So yes φ(1)=1. For n>1: compute φ = n. For each prime factor p: φ = φ * (1 - 1/p) = φ / p * (p-1). Use integer arithmetic. Need to avoid floating point. So we can do: for each p, result = result / p * (p-1). But careful: result may be large (up to 1e9) but within 64-bit. So use long long. Steps:
- Input n.
- If n == 1: output 1.
- else:
- ans = n.
- For i from 2 to sqrt(n):
- if n % i == 0:
ans = ans / i * (i-1)
while n % i == 0: n /= i
- after loop, if n > 1: ans = ans / n * (n-1) // n is the remaining prime factor > sqrt(original).
- Output ans.
Edge Cases: n large prime near 1e9, sqrt ~31623, loop will go through many numbers but no factor found. Then after loop, n>1, ans = original_n / n * (n-1) = n / n * (n-1) = n-1. Good.
Complexity: O(sqrt(original_n)) but we reduce n as we divide, so the worst-case is sqrt of original n. For 1e9, sqrt=31623, that's fine.
But wait: n can be 1e9, which is 10^9. sqrt(1e9) = 31622.776, so loop up to 31623 is fine. However, there is also possible optimization: check 2 separately, then odd numbers from 3. But not necessary.
But careful: In the loop, we need to use i up to sqrt(current_n)? Since we are dividing n, we can update bound to sqrt(n) dynamically, but it's fine to use original sqrt. However, if we reduce n, we could break early if ii > n? Possibly better to recompute sqrt. But it's okay to run up to sqrt(original_n) but ensure we don't try to divide by i that is > current n? Actually if i > current n, then ii > original? No, original sqrt bound is fixed; but if n becomes small, we still iterate many i values unnecessarily. But n can be up to 1e9, but after factoring out small primes, n may become 1, but then we still have to iterate from i up to sqrt(original) but we can break when ii > n? That would be efficient. Let's implement: while (i * i <= n) { ... }. But we need to update the condition as n reduces. So better: for i = 2; ii <= n; i+=... Actually using a while loop: i=2; while(i*i <= n) { ...; i+= (i==2)?1:2} but careful: after dividing out i, n reduces, so the bound decreases, but we still increment i. That's fine. But if we break early when n==1, we can break out.
Alternatively, we can factor by trial dividing up to sqrt(original). For speed, it's fine. For worst-case prime (no factor), we check up to sqrt(n) which is 31623; that's trivial.
Thus solution is straightforward.
Now produce final answer: Provide code in C++ or something? The problem statement didn't specify language. Usually these problems expect a program. We need to output just the number. We'll produce code.
Potential pitfalls: integer overflow: ans = ans / i * (i-1). Since ans <= 1e9, ans/i <= 5e8, times (i-1) ≤ 1e9, within 32-bit? Actually 1e9 times (i-1) might be > 2^31-1 if ans large. But ans is reduced as we divide. But initial ans = n up to 1e9. When we do ans / i * (i-1), if i is a factor, ans is divisible by i exactly because ans = original_n and we haven't changed ans yet. However, after dividing by some prime factor, ans becomes ans' which is not necessarily a multiple of the next prime factor? But the formula for totient: φ(n) = n * ∏{p|n} (1 - 1/p). If we compute sequentially: ans = n; for each prime factor p: ans = ans / p * (p-1). This is valid because ans will always be divisible by p at that step? At the first step, ans = n which is divisible by p because p divides n. After dividing by p and multiplying by (p-1), the new ans is not necessarily divisible by the next prime factor q, but the algorithm still works: we don't require divisibility at later steps; we just compute the product. But we are using integer arithmetic: ans / p * (p-1). This requires that ans is divisible by p. Is ans always divisible by p when we encounter p? Initially ans = original n, and we remove prime factors in increasing order. But note: after we process p, we modify ans, and then we continue to find other prime factors. However, the formula says: φ(n) = n * ∏{p|n} (1 - 1/p). If we compute it as ans = n; for each p: ans = ans * (p-1) / p, we must ensure that the multiplication by (p-1) happens before division by p? Actually order doesn't matter as long as we use integer division correctly. If we do ans = ans / p * (p-1), then we need ans divisible by p. But is ans divisible by p at that moment? Initially ans = n, and p divides n, so yes. After processing one prime, ans = n / p * (p-1). Now, does the next prime factor q divide this new ans? Not necessarily. But we are not dividing by q at this step; we are using the fact that φ(n) = n * (p-1)/p * (q-1)/q. We can compute sequentially: start with n, multiply by (p-1)/p, then multiply by (q-1)/q. The multiplication by (q-1)/q is not applied to n directly but to the intermediate value. However, (q-1)/q is a fraction. To avoid fractions, we multiply by (q-1) then divide by q. But the intermediate value may not be divisible by q. For example: n = 6 = 23. Compute φ(6) = 2. If we process p=2 first: ans = 6 / 2 * 1 = 3. Then process p=3: ans = 3 / 3 * 2 = 2. Here 3 is divisible by 3. Good. Another example: n = 12 = 2^2 * 3. φ(12) = 12 * (1-1/2)(1-1/3)=121/22/3=4. Process p=2 (first occurrence): ans = 12 / 2 * 1 = 6. But note: there is still factor 2^1 left? Actually we should process each distinct prime only once. In the loop, we find p=2, we divide n by 2 while n%2==0 but we only apply ans adjustment once. That is correct: we should multiply by (1-1/p) once per distinct prime. So after handling p=2, ans = 12/21=6. Now n after division: we divided out all 2's: n becomes 3. Then we find p=3: ans = 6 / 3 * 2 = 4. Here 6 is divisible by 3. Good. Another example: n = 30 = 235. φ=8. Process p=2: ans = 30/21=15. Process p=3: ans = 15/34=8. Each intermediate divisible. Another: n=105=37, φ=48. Process p=3: ans=105/36=6. Works. n = 3 * 5 =15, φ=8. Process 3: ans=15/34=8. Works. n = 2 * 3 * 7 =42, φ=12. Process 2: ans=21; 3: ans=21/36=12. Works. So far good.
But can there be a case where intermediate ans is not divisible by the next prime? Let's try n = 4 * 9? Actually distinct primes only. Suppose n = p * q where p and q are primes, p<q. Then ans after first: n/p*(p-1) = q*(p-1). Is q*(p-1) divisible by q? Obviously yes because factor q. So always divisible. In general, after processing primes p1,...,pk-1, ans_current = (n / (∏{i=1}^{k-1} p_i)) * ∏{i=1}^{k-1} (p_i-1). But n = (∏{i=1}^{m} p_i^{a_i}) * maybe other primes. Since we haven't processed p_k yet, n still contains p_k^{a_k} factor. So n includes factor p_k^{a_k}. And we divided by ∏{i=1}^{k-1} p_i (each once? Actually we did ans = ans / p_i * (p_i-1). That division by p_i only once, not p_i^{a_i}. But the original n contains p_i^{a_i}. In ans = n * ∏{j=1}^{k-1} (p_j-1)/p_j = n / (∏{j=1}^{k-1} p_j) * ∏{j=1}^{k-1} (p_j-1). In n, for each p_j we have at least one factor p_j (since a_j >=1). So n / (∏{j=1}^{k-1} p_j) is integer and still contains p_k^{a_k} factor (since we only removed one copy of each p_j, but we may have removed more? Actually we removed exactly one copy of each p_j because we only divided by p_j once. But in n, there are a_j copies of p_j. After dividing by p_j once, we still have p_j^{a_j-1} factor left in n? Wait: we are not directly dividing n; we are dividing ans, which started as n. When we do ans = ans / p_i, we are dividing the current ans by p_i. The current ans contains the full product n at that time, including all powers of primes. However, after we divide by p_i once, the exponent of p_i in ans reduces by 1. But we only care about divisibility by p_k later. The current ans after processing some primes still includes the full factors of p_k (since p_k hasn't been touched). So ans_current contains at least p_k^{a_k} factor. Also, after dividing by each previous prime p_i once, the exponent of p_i is reduced from a_i to a_i-1, but that's irrelevant. So ans_current = p_k^{a_k} * (other factors). So clearly ans_current is divisible by p_k. So it's safe. Even if a_k = 1, it's still divisible by p_k. So yes, ans_current always divisible by p_k. Good.
Thus algorithm works.
Implementation details:
We can handle n=1 separately.
For factorization: iterate i=2; i*i <= n; i+=... (but careful: n changes as we divide out factors). Use a loop:
long long n;
cin >> n;
long long ans = n;
long long temp = n; // but we will modify n.
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n > 1) {
ans = ans / n * (n - 1);
}
Note: When we modify n by dividing out factors, the loop condition ii <= n will become false earlier for large i when n becomes small, which is good for performance. However, we need to be careful: the variable 'n' is being reduced, so the loop bound shrinks. That's fine as we only need to check up to sqrt(current n). But we need to also consider that we might miss factors if we stop too early? For example, suppose original n = pq where p and q are both > sqrt(original n). But that's impossible because if both > sqrt, product > n. So at most one prime factor > sqrt. So after dividing out all small primes, if remaining n > 1, it's a prime factor (or a power of a prime > sqrt? Actually if the remaining n is > 1, it could be a prime or a product of primes > sqrt? Cannot be product of two > sqrt because product would exceed n. Could be a power of a prime > sqrt, like p^2 where p > sqrt(original)? Then p^2 > original? Actually if p > sqrt(original), then p^2 > original, impossible unless exponent 1. So remaining n is a prime. So fine.
Thus code is correct.
Now produce final answer: The problem expects just the number in a line.
We'll provide a C++ solution.
Edge Cases:
- n=1: φ(1)=1.
- n=0? not allowed (n≥1).
- n=1000000000 = 10^9 = 2^9 * 5^9? Actually 10^9 = (25)^9 = 2^9 * 5^9. So φ(10^9) = 10^9 * (1-1/2)(1-1/5) = 10^9 * 1/2 * 4/5 = 10^9 * 2/5 = 400,000,000. Indeed example: Input 1000000, output 400000. That matches φ(10^6)= 10^6 * (1/2)*(4/5)= 400,000. So example matches.
Now, we need to produce answer as a solution.
Potential pitfalls: use long long for ans and n to avoid overflow when multiplying? ans is up to 1e9, i-1 up to 1e9? i up to sqrt(1e9) ~31623, so i-1 small, multiplication safe with int. But after first step ans may become less than original. However, when we do ans = ans / n * (n-1) in the final step, n could be large up to 1e9, ans up to 1e9, division yields integer, multiplication yields up to 1e9 * (n-1)/n? Actually it's safe. Use long long for safety.
Now produce final answer.
But wait: The problem statement says: "count the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n." Usually totatives are numbers <= n that are coprime to n. That's φ(n). So output φ(n). So we implement.
Now provide code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = n;
long long original_n = n; // not needed, but okay.
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n > 1) {
ans = ans / n * (n - 1);
}
cout << ans << endl;
return 0;
}
But careful: For n=1, the loop condition i*i<=n fails (i=2, 4<=1 false), so n>1? n=1 so not >1, ans remains n=1, output 1, correct.
So no special case needed. Indeed ans initialized to n=1, no factors, n>1 false, output 1. Good.
Thus final answer is just this.
Now produce the final message: Explanation and code.
But the user request: "For given integer n, count the totatives of n ... Input: n Output: The number of totatives in a line." They may be expecting a solution. So I'll provide the answer: Provide C++ code.
Given the context, I'd produce a concise explanation and the code.
Now final. ```cpp
#include
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = n;
// factorize n
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
ans = ans / i * (i - 1); // multiply by (1 - 1/i)
while (n % i == 0) n /= i; // remove all factors of i
}
}
// if a prime factor remains ( > sqrt(original n) )
if (n > 1) {
ans = ans / n * (n - 1);
}
cout << ans << endl;
return 0;
}