Skip to content

Commit eba88a7

Browse files
authored
Merge pull request #425 from AlgorithmWithGod/khj20006
[20250709] BOJ / G1 / 서로소 / 권혁준
2 parents 8a0f47c + 59d7723 commit eba88a7

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static long A, B, N;
57+
static List<Long> list;
58+
59+
public static void main(String[] args) throws Exception {
60+
61+
io = new IOController();
62+
63+
int TC = io.nextInt();
64+
for(int tc=1;tc<=TC;tc++) {
65+
io.write("Case #" + tc + ": ");
66+
init();
67+
solve();
68+
}
69+
70+
io.close();
71+
72+
}
73+
74+
public static void init() throws Exception {
75+
76+
A = io.nextLong();
77+
B = io.nextLong();
78+
N = io.nextLong();
79+
80+
}
81+
82+
static void solve() throws Exception {
83+
84+
list = new ArrayList<>();
85+
for(long i=2;i*i<=N;i++) if(N%i == 0) {
86+
while(N%i == 0) N/=i;
87+
list.add(i);
88+
}
89+
if(N != 1) list.add(N);
90+
91+
io.write((f(B) - f(A-1)) + "\n");
92+
93+
}
94+
95+
static long f(long x) {
96+
97+
long res = x;
98+
int size = list.size();
99+
for(int i=1;i<(1<<size);i++) {
100+
int cnt = 0;
101+
long v = 1;
102+
for(int j=0;j<size;j++) if((i & (1<<j)) != 0) {
103+
v *= list.get(j);
104+
cnt++;
105+
}
106+
if(cnt%2 == 1) res -= x/v;
107+
else res += x/v;
108+
}
109+
return res;
110+
111+
}
112+
113+
}
114+
```

0 commit comments

Comments
 (0)