Skip to content

Commit 7df3cba

Browse files
authored
[20250308] BOJ / G2 / MEX / 권혁준
1 parent 401bd3e commit 7df3cba

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

khj20006/202503/08 BOJ G2 MEX.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## JAVA
2+
```java
3+
4+
import java.util.*;
5+
import java.io.*;
6+
7+
8+
class Main {
9+
10+
// IO field
11+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
static StringTokenizer st;
14+
15+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
16+
static int nextInt() {return Integer.parseInt(st.nextToken());}
17+
static long nextLong() {return Long.parseLong(st.nextToken());}
18+
static void bwEnd() throws Exception {bw.flush();bw.close();}
19+
20+
// Additional field
21+
22+
static int N;
23+
static boolean[] A = new boolean[2200001];
24+
static boolean[] B = new boolean[2200001];
25+
26+
public static void main(String[] args) throws Exception {
27+
28+
ready();
29+
solve();
30+
31+
bwEnd();
32+
33+
}
34+
35+
static void ready() throws Exception{
36+
37+
N = Integer.parseInt(br.readLine());
38+
for(nextLine();N-->0;A[nextInt()]=true);
39+
40+
}
41+
42+
static void solve() throws Exception{
43+
44+
for(int i=0;i<=2200000;i++) {
45+
if(!A[i] && !B[i]){
46+
bw.write(i + "\n");
47+
return;
48+
}
49+
if(i<2 || !A[i]) continue;
50+
for(long j=i*2;j<=Math.min(2200000L,(long)i*i);j+=i) B[(int)j] = true;
51+
}
52+
53+
}
54+
55+
}
56+
57+
```
58+
59+
## C++
60+
```cpp
61+
62+
#include <iostream>
63+
#include <bitset>
64+
using namespace std;
65+
using ll = long long;
66+
67+
int main() {
68+
cin.tie(0)->sync_with_stdio(0);
69+
70+
int N;
71+
cin>>N;
72+
bitset<2200001> A, B;
73+
for(int a;N--;A[a]=1) cin>>a;
74+
for(int i=0;i<2200001;i++) {
75+
if(!A[i] && !B[i]) return cout<<i,0;
76+
if(i < 2 || !A[i]) continue;
77+
for(ll j=i*2;j<=min(2200000LL, (ll)i*i);j+=i) B[j] = 1;
78+
}
79+
80+
}
81+
82+
```

0 commit comments

Comments
 (0)