UVa 10820 - Send a Table (Eulerian function)

Link:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1761

 

Title:

There is a competition problem, input two integers x, y (1≤x, y≤n), and output a certain function f(x,y).
A contestant wants to submit the table (that is, calculate all f(x,y) in advance and write it in the source code), but the table is too large, and the source code exceeds the limit of the competition, so it needs to be simplified.
That problem has a property that it is easy to calculate f(x*k, y*k) (k is any positive integer) based on f(x,y), so that some f(x,y) do not need to be saved .
Enter n (n≤50000), your task is to count how many elements are in the simplest table. For example, when n=2 there are 3: (1,1), (1,2), (2,1).

 

analyze:

The essence of this question is: input n, how many binary groups (x, y) satisfy: 1≤x, y≤n, and x and y are mutually prime.
It is not difficult to find that x and y are not equal in other two-tuples (x, y) except (1, 1). Suppose there are f(n) pairs of pairs satisfying x<y, then the answer is 2f(n)+1.
Comparing with the definition of Euler function, we can get f(n)=phi(2)+phi(3)+…+phi(n).

 

Code:

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     static final int UP = 50000;
 6     static int phi[] = new int[UP+5];
 7     
 8     static void constant() {
 9         for(int t = 2; t <= UP; t++) if(phi[t] == 0) {
10             for(int i = t; i <= UP; i += t) {
11                 if(phi[i] == 0) phi[i] = i;
12                 phi[i] = phi[i] / t * (t-1);
13             }
14         }
15         for(int i = 3; i <= UP; i++) phi[i] += phi[i-1];
16     }
17     
18     public static void main(String args[]) {
19         Scanner cin = new Scanner(new BufferedInputStream(System.in));
20         constant();
21         
22         while(true) {
23             int n = cin.nextInt();
24             if(n == 0) break;
25             System.out.println(2 * phi[n] + 1);
26         }
27         cin.close();
28     }
29 }

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326855701&siteId=291194637