Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main implements Runnable {
- private void solution() throws IOException {
- while (true) {
- int n = in.nextInt();
- if (n == 0) {
- break;
- }
- out.println(solve(n));
- }
- }
- private int brute(int n) {
- int res = 0;
- for (int i = 1; i <= n; ++i) {
- int g = gcd(i, n);
- if (1 < g && g < i) {
- ++res;
- }
- }
- return res;
- }
- private int solve(int n) {
- int res = phi(n);
- int factors = 0;
- for (int i = 1; i * i <= n; ++i) {
- if (n % i == 0) {
- ++factors;
- if (i * i != n) {
- ++factors;
- }
- }
- }
- return n - (res + factors - 1);
- }
- private int phi(int n) {
- int res = n;
- for (int i = 2; i * i <= n; ++i) {
- if (n % i == 0) {
- while (n % i == 0) {
- n /= i;
- }
- res -= res / i;
- }
- }
- if (n > 1) {
- res -= res / n;
- }
- return res;
- }
- private int gcd(int a, int b) {
- return b == 0 ? a : gcd(b, a % b);
- }
- @Override
- public void run() {
- try {
- solution();
- in.reader.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- private class Scanner {
- BufferedReader reader;
- StringTokenizer tokenizer;
- public Scanner(Reader reader) {
- this.tokenizer = new StringTokenizer("");
- this.reader = new BufferedReader(reader);
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String line = reader.readLine();
- if (line == null) {
- return false;
- }
- tokenizer = new StringTokenizer(line);
- }
- return true;
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public String nextLine() throws IOException {
- tokenizer = new StringTokenizer("");
- return reader.readLine();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(next());
- }
- }
- public static void main(String[] args) {
- new Thread(null, new Main(), "Main", 1 << 28).start();
- }
- Scanner in = new Scanner(new InputStreamReader(System.in));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement