Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main implements Runnable {
  5.    
  6.     private void solution() throws IOException {
  7.         while (true) {
  8.             int n = in.nextInt();
  9.             if (n == 0) {
  10.                 break;
  11.             }
  12.             out.println(solve(n));
  13.         }
  14.     }
  15.  
  16.     private int brute(int n) {
  17.         int res = 0;
  18.         for (int i = 1; i <= n; ++i) {
  19.             int g = gcd(i, n);
  20.             if (1 < g && g < i) {
  21.                 ++res;
  22.             }
  23.         }
  24.         return res;
  25.     }
  26.    
  27.     private int solve(int n) {
  28.         int res = phi(n);
  29.         int factors = 0;
  30.         for (int i = 1; i * i <= n; ++i) {
  31.             if (n % i == 0) {
  32.                 ++factors;
  33.                 if (i * i != n) {
  34.                     ++factors;
  35.                 }
  36.             }
  37.         }
  38.         return n - (res + factors - 1);
  39.     }
  40.  
  41.     private int phi(int n) {
  42.         int res = n;
  43.         for (int i = 2; i * i <= n; ++i) {
  44.             if (n % i == 0) {
  45.                 while (n % i == 0) {
  46.                     n /= i;
  47.                 }
  48.                 res -= res / i;
  49.             }
  50.         }
  51.         if (n > 1) {
  52.             res -= res / n;
  53.         }
  54.         return res;
  55.     }
  56.  
  57.     private int gcd(int a, int b) {
  58.         return b == 0 ? a : gcd(b, a % b);
  59.     }
  60.  
  61.     @Override
  62.     public void run() {
  63.         try {
  64.             solution();
  65.             in.reader.close();
  66.             out.close();
  67.         } catch (IOException e) {
  68.             e.printStackTrace();
  69.             System.exit(1);
  70.         }
  71.     }
  72.    
  73.     private class Scanner {
  74.         BufferedReader reader;
  75.         StringTokenizer tokenizer;
  76.        
  77.         public Scanner(Reader reader) {
  78.             this.tokenizer = new StringTokenizer("");
  79.             this.reader = new BufferedReader(reader);
  80.         }
  81.        
  82.         public boolean hasNext() throws IOException {
  83.             while (!tokenizer.hasMoreTokens()) {
  84.                 String line = reader.readLine();
  85.                 if (line == null) {
  86.                     return false;
  87.                 }
  88.                 tokenizer = new StringTokenizer(line);
  89.             }
  90.             return true;
  91.         }
  92.        
  93.         public String next() throws IOException  {
  94.             hasNext();
  95.             return tokenizer.nextToken();
  96.         }
  97.        
  98.         public String nextLine() throws IOException {
  99.             tokenizer = new StringTokenizer("");
  100.             return reader.readLine();
  101.         }
  102.        
  103.        
  104.         public int nextInt() throws IOException {
  105.             return Integer.parseInt(next());
  106.         }
  107.        
  108.     }
  109.     public static void main(String[] args) {
  110.         new Thread(null, new Main(), "Main", 1 << 28).start();
  111.     }
  112.  
  113.     Scanner in = new Scanner(new InputStreamReader(System.in));
  114.     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement