Skip to content

Commit f25fa68

Browse files
authored
Merge pull request #229 from AlgorithmWithGod/khj20006
[20250311] BOJ / P5 / 마법의 구슬 / 권혁준
2 parents 4a54dc0 + 0c2f841 commit f25fa68

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class SegTree{
7+
int[] tree;
8+
SegTree(int size){
9+
tree = new int[size*4 + 1];
10+
}
11+
void upt(int s, int e, int i, int n) {
12+
if(s == e) {
13+
tree[n]++;
14+
return;
15+
}
16+
int m=(s+e)>>1;
17+
if(i<=m) upt(s,m,i,n*2);
18+
else upt(m+1,e,i,n*2+1);
19+
tree[n] = tree[n*2] + tree[n*2+1];
20+
}
21+
int find(int s, int e, int l, int r, int n) {
22+
if(l>r || l>e || r<s) return 0;
23+
if(l<=s && e<=r) return tree[n];
24+
int m=(s+e)>>1;
25+
return find(s,m,l,r,n*2) + find(m+1,e,l,r,n*2+1);
26+
}
27+
}
28+
29+
class Main {
30+
31+
// IO field
32+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
33+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
34+
static StringTokenizer st;
35+
36+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
37+
static int nextInt() {return Integer.parseInt(st.nextToken());}
38+
static long nextLong() {return Long.parseLong(st.nextToken());}
39+
static void bwEnd() throws Exception {bw.flush();bw.close();}
40+
41+
// Additional field
42+
43+
static long S, F, M;
44+
static long[] C;
45+
static boolean[] sieve;
46+
47+
public static void main(String[] args) throws Exception {
48+
49+
ready();
50+
solve();
51+
52+
bwEnd();
53+
54+
}
55+
56+
static void ready() throws Exception{
57+
58+
nextLine();
59+
S = nextLong();
60+
F = nextLong();
61+
M = nextLong();
62+
F += S;
63+
sieve = new boolean[(int)M+1];
64+
C = new long[(int)M+1];
65+
66+
}
67+
68+
static void solve() throws Exception{
69+
70+
for(int i=2;i*i<=M;i++) if(!sieve[i]) for(int j=i*i;j<=M;j+=i) sieve[j] = true;
71+
72+
for(int i=2;i<=M;i++) if(!sieve[i]) for(long j=i;j<=F;j*=i) C[i] += F/j;
73+
for(int i=2;i<=M;i++) if(!sieve[i]) for(long j=i;j<=S;j*=i) C[i] -= S/j;
74+
for(int i=2;i<=M;i++) if(!sieve[i]) for(long j=i;j<=F-S;j*=i) C[i] -= (F-S)/j;
75+
76+
int ans = 1;
77+
for(int i=2;i<=M;i++) {
78+
if(!sieve[i]) {
79+
if(C[i] > 0) ans = i;
80+
continue;
81+
}
82+
int temp = i, cnt = 0;
83+
boolean can = true;
84+
for(int j=2;j*j<=temp;j++) if(!sieve[j] && temp%j == 0) {
85+
cnt = 0;
86+
while(temp%j == 0) {
87+
temp/=j;
88+
cnt++;
89+
}
90+
can &= C[j] >= cnt;
91+
cnt = 0;
92+
}
93+
if(temp != 1) can &= C[temp] >= 1;
94+
if(can) ans = i;
95+
}
96+
bw.write(ans + "\n");
97+
98+
}
99+
100+
}
101+
102+
```

0 commit comments

Comments
 (0)