Skip to content

Commit 8b92f9a

Browse files
authored
Merge pull request #355 from GreatAlgorithm-Study/dahye
[27์ฃผ์ฐจ] ๊ณ ๋‹คํ˜œ
2 parents 6a59651 + 535815a commit 8b92f9a

File tree

5 files changed

+471
-0
lines changed

5 files changed

+471
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.io.*;
2+
3+
/*
4+
* ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ
5+
*/
6+
7+
public class DH_1644 {
8+
public static void main(String[] args) throws Exception {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
int N = Integer.parseInt(br.readLine());
12+
13+
// N๊นŒ์ง€ ์ˆซ์ž ์ค‘์—์„œ ์–ด๋–ค ๊ฒƒ์ด ์†Œ์ˆ˜์ธ์ง€ ์•Œ์•„๋‚ด๊ธฐ
14+
boolean[] isNotPrime = getNotPrime(N);
15+
16+
System.out.println(getCount(isNotPrime, N));
17+
}
18+
19+
// ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•ด ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ์„ ๊ตฌํ–ˆ์„ ๋•Œ, n์ด ๋˜๋Š” ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
20+
static int getCount(boolean[] isNotPrime, int n) {
21+
int count = 0;
22+
23+
int s = 2, e = 2, sum = 2;
24+
25+
while(s <= e && e < isNotPrime.length) {
26+
// e ์˜ฎ๊ธฐ๊ธฐ
27+
if(sum < n) {
28+
29+
while(e < isNotPrime.length) {
30+
31+
e += 1;
32+
33+
if(isNotPrime.length == e) break;
34+
35+
if(!isNotPrime[e]) {
36+
sum += e;
37+
break;
38+
}
39+
}
40+
}
41+
42+
// sum >= n โ†’ s ์˜ฎ๊ธฐ๊ธฐ
43+
else {
44+
if(sum == n) count += 1;
45+
46+
sum -= s;
47+
48+
while(s < e) {
49+
s += 1;
50+
51+
if(!isNotPrime[s]) {
52+
break;
53+
}
54+
}
55+
}
56+
}
57+
58+
return count;
59+
}
60+
61+
// ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค ์ฒด๋กœ n๊นŒ์ง€ ์ˆซ์ž ์ค‘์—์„œ ์†Œ์ˆ˜ ๊ฑธ๋Ÿฌ๋‚ด๊ธฐ
62+
static boolean[] getNotPrime(int n) {
63+
boolean[] isNotPrime = new boolean[n + 1];
64+
65+
isNotPrime[0] = true;
66+
isNotPrime[1] = true;
67+
68+
for(int i = 2; i <= Math.sqrt(isNotPrime.length); i++) {
69+
if(isNotPrime[i]) continue;
70+
71+
for(int j = i * i; j < isNotPrime.length; j += i) {
72+
isNotPrime[j] = true;
73+
}
74+
}
75+
76+
return isNotPrime;
77+
}
78+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
3+
/*
4+
* ํƒ€์ผ ์ฑ„์šฐ๊ธฐ 3
5+
*/
6+
7+
public class DH_14852 {
8+
static final int MOD = 1_000_000_007;
9+
10+
public static void main(String[] args) throws Exception {
11+
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
int N = Integer.parseInt(br.readLine());
14+
15+
long[] dp = new long[Math.max(4, N + 1)];
16+
17+
dp[1] = 2;
18+
dp[2] = 7;
19+
20+
long sum = 2; // ๊ธฐ๋ณธ์œผ๋กœ ๋”ํ•ด์ง€๋Š”๊ฑฐ
21+
22+
for(int i = 3; i < dp.length; i++) {
23+
// sum ์—๋Š” dp[i - 3] * 2 + dp[i - 4] * 2 + dp[i - 5] * 2 ...๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์Œ
24+
dp[i] = (dp[i - 1] * 2 + dp[i - 2] * 3 + sum) % MOD;
25+
sum += dp[i - 2] * 2;
26+
}
27+
28+
System.out.println(dp[N] % MOD);
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ์ƒ˜ํ„ฐ
6+
* ์ฃผ์˜์ 
7+
* 1. ์ƒ˜ํ„ฐ์˜ ์œ„์น˜๋Š” -100_000_000 ~ 100_000_000์ด์ง€๋งŒ, ์ง‘์˜ ์œ„์น˜๋Š” ์ด๊ฑธ ๋ฒ—์–ด๋‚  ์ˆ˜ ์žˆ์Œ
8+
* 2. ๋ถˆํ–‰๋„๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ, ๋ถˆํ–‰๋„์˜ ๋ฒ”์œ„ long์œผ๋กœ ์„ค์ •ํ•˜๊ธฐ
9+
* - 1 + 2 + ... + 500,000 = 125,000,250,000
10+
*/
11+
12+
public class DH_18513 {
13+
14+
static final int INF = 100_200_000;
15+
16+
public static void main(String[] args) throws Exception {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
int N = Integer.parseInt(st.nextToken()); // ์ƒ˜ํ„ฐ์˜ ๊ฐœ์ˆ˜
21+
int K = Integer.parseInt(st.nextToken()); // ์ง‘์˜ ๊ฐœ์ˆ˜
22+
23+
boolean[] v = new boolean[2 * INF + 1]; // -INF ~ INF
24+
Queue<int[]> q = new ArrayDeque<int[]>();
25+
26+
st = new StringTokenizer(br.readLine()); // ์ƒ˜ํ„ฐ์— ๋Œ€ํ•œ ์ •๋ณด
27+
for(int i = 0; i < N; i++) {
28+
int current = Integer.parseInt(st.nextToken());
29+
q.add(new int[] {current, 0});
30+
v[current + INF] = true;
31+
}
32+
33+
int cnt = 0;
34+
int badCnt = 0;
35+
36+
L: while(!q.isEmpty()) {
37+
int[] current = q.poll();
38+
39+
// ์ขŒ, ์šฐ ํ™•์ธํ•˜๊ธฐ
40+
for(int d = -1; d < 2; d += 2) {
41+
int next = current[0] + d;
42+
43+
if(next < -INF || next > INF || v[next + INF]) continue;
44+
cnt += 1;
45+
badCnt += current[1] + 1;
46+
47+
if(cnt ==K) break L;
48+
49+
q.add(new int[] {next, current[1] + 1});
50+
v[next + INF] = true;
51+
}
52+
}
53+
54+
System.out.println(badCnt);
55+
}
56+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ์กฐ์‹ฌํ•ด์•ผ ๋๋˜ ๋ถ€๋ถ„
6+
* - ๋‚˜๋ฌด๊ฐ€ ์„ฑ์žฅํ•˜๊ณ  ๋ฒˆ์‹ํ•  ๋•Œ, ๋ฒฝ์˜ ์ •๋ณด๊ฐ€ ์—†์–ด์ง€๋ฉด ์•ˆ๋์Œ!
7+
*/
8+
9+
public class DH_๋‚˜๋ฌด๋ฐ•๋ฉธ {
10+
11+
static int N, M, K, C;
12+
static int totalRemoveTreeCnt, tmpRemoveTreeCnt;
13+
static int[][] map, remove; // map: ๋‚˜๋ฌด, remove: ์ œ์ดˆ์ œ ์ง€์† ์‹œ๊ฐ„
14+
static int[] dr = {-1, -1, -1, 0, 1, 1, 1, 0}, dc = {-1, 0, 1, 1, 1, 0, -1, -1}; // 8๋ฐฉ ๋ฐฐ์—ด
15+
16+
public static void main(String[] args) throws Exception {
17+
initInput();
18+
solution();
19+
System.out.println(totalRemoveTreeCnt);
20+
}
21+
22+
static void solution() {
23+
24+
while(M-- > 0) {
25+
grow(); // ์„ฑ์žฅ
26+
spread(); // ๋ฒˆ์‹
27+
remove(); // ์ œ์ดˆ์ œ ๋ฟŒ๋ฆฌ๊ธฐ
28+
decreaseRemove(); // ์ œ์ดˆ์ œ ๊ฐ์†Œ
29+
}
30+
}
31+
32+
// ์ œ์ดˆ์ œ ์ง€์† ์‹œ๊ฐ„ 1์”ฉ ์ค„์ด๊ธฐ
33+
static void decreaseRemove() {
34+
for(int r = 0; r < remove.length; r++) {
35+
for(int c = 0; c < remove[0].length; c++) {
36+
if(remove[r][c] == 0) continue;
37+
remove[r][c] -= 1;
38+
}
39+
}
40+
}
41+
42+
static void remove() {
43+
// ์ œ์ดˆ์ œ๊ฐ€ ๋ฟŒ๋ ค์ง„ ์นธ์—๋Š” C๋…„ ๋งŒํผ ์ œ์ดˆ์ œ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๊ฐ€ C + 1๋…„์งธ๊ฐ€ ๋  ๋•Œ ์‚ฌ๋ผ์ง
44+
int removeTreeCnt = -1;
45+
46+
Queue<Integer> spreadPos = new ArrayDeque<Integer>(); // ์ œ์ดˆ์ œ๊ฐ€ ํผ์ ธ ๋‚˜๊ฐˆ ์œ„์น˜
47+
48+
for(int r = 0; r < map.length; r++) {
49+
for(int c = 0; c < map[0].length; c++) {
50+
if(map[r][c] == -1) continue; // ๋ฒฝ์ด๋ฉด continue
51+
52+
tmpRemoveTreeCnt = 0; // (r, c)์—์„œ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋‚˜๋ฌด ์ˆ˜
53+
54+
Queue<Integer> tmp = getRemoveCnt(r, c);
55+
56+
if(tmpRemoveTreeCnt <= removeTreeCnt) continue;
57+
removeTreeCnt = tmpRemoveTreeCnt;
58+
59+
spreadPos = tmp;
60+
}
61+
}
62+
63+
totalRemoveTreeCnt += removeTreeCnt;
64+
65+
// ์ œ์ดˆ์ œ ๋ฟŒ๋ ค์ฃผ๊ธฐ
66+
while(!spreadPos.isEmpty()) {
67+
int pos = spreadPos.poll();
68+
69+
int r = pos / N, c = pos % N;
70+
if(map[r][c] == -1) continue; // ๋ฒฝ์ผ ๊ฒฝ์šฐ ๋„˜์–ด๊ฐ€์•ผ ๋จ!!โœจ
71+
72+
map[r][c] = 0;
73+
remove[r][c] = (C + 1); // ์ œ์ดˆ์ œ ๋ฟŒ๋ฆฌ๊ธฐ
74+
}
75+
}
76+
77+
static Queue<Integer> getRemoveCnt(int r, int c) {
78+
Queue<Integer> spreadPos = new ArrayDeque<>();
79+
80+
// (r, c)์—์„œ ๋Œ€๊ฐ์„  4๋ฐฉ์œผ๋กœ K์นธ ๋งŒํผ ๊ฐ€๊ธฐ
81+
Queue<int[]> q = new ArrayDeque<>();
82+
83+
int pos = r * N + c;
84+
85+
// (r, c) ์ง€์ ์—์„œ๋Š” ๋Œ€๊ฐ์„  4๋ฐฉ์œผ๋กœ ๋ณด๋‚ด๊ธฐ
86+
for(int dir = 0; dir < 8; dir += 2) {
87+
q.add(new int[] {pos, 0, dir});
88+
}
89+
90+
spreadPos.add(pos); // ์ œ์ดˆ์ œ๊ฐ€ ํผ์ง€๋Š” ์œ„์น˜
91+
tmpRemoveTreeCnt += map[r][c];
92+
93+
while(!q.isEmpty()) {
94+
95+
int[] current = q.poll();
96+
int cr = current[0] / N;
97+
int cc = current[0] % N;
98+
int dir = current[2];
99+
100+
if(map[cr][cc] == 0) continue; // ์ฒ˜์Œ์— (r, c)๊ฐ€ ๋นˆ ๊ณต๊ฐ„์œผ๋กœ ์ฃผ์–ด์กŒ์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฑฐ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ๋จ
101+
102+
int nr = cr + dr[dir];
103+
int nc = cc + dc[dir];
104+
105+
// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๋‚˜๋ฌด๊ฐ€ ์•„์˜ˆ ์—†๋Š” ์นธ์ด๊ฑฐ๋‚˜, ๋ป—์–ด๊ฐ„ ๊ฑฐ๋ฆฌ๊ฐ€ K๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด continue
106+
if(!check(nr, nc) || current[1] == K) continue;
107+
108+
int nPos = nr * N + nc;
109+
spreadPos.add(nPos);
110+
111+
// ๋ฒฝ์ด ์žˆ๊ฑฐ๋‚˜, ๋‚˜๋ฌด๊ฐ€ ์•„์˜ˆ ์—†๋Š” ์นธ์€ ๊ทธ ์นธ ๊นŒ์ง€๋งŒ ์ œ์ดˆ์ œ๊ฐ€ ๋ฟŒ๋ ค์ง
112+
if(map[nr][nc] <= 0) continue;
113+
114+
q.add(new int[] {nPos, current[1] + 1, dir});
115+
tmpRemoveTreeCnt += map[nr][nc];
116+
}
117+
return spreadPos;
118+
}
119+
120+
// ๋ฒˆ์‹ - ๋ฒฝ, ๋‹ค๋ฅธ ๋‚˜๋ฌด, ์ œ์ดˆ์ œ ๋ชจ๋‘ ์—†๋Š” ์นธ์— ๋ฒˆ์‹ ์ง„ํ–‰ (๋ฒˆ์‹์ด ๊ฐ€๋Šฅํ•œ ์นธ์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋‚˜๋ˆ„์–ด์ง„ ๊ทธ๋ฃจ ์ˆ˜ ๋งŒํผ ๋ฒˆ์‹)
121+
static void spread() {
122+
int[][] tmp = new int[N][N];
123+
124+
for(int r = 0; r < map.length; r++) {
125+
for(int c = 0; c < map[0].length; c++) {
126+
if(map[r][c] <= 0) continue;
127+
128+
int cnt = 0; // ๋ฒˆ์‹์ด ๊ฐ€๋Šฅํ•œ ์นธ์˜ ๊ฐœ์ˆ˜
129+
130+
for(int d = 1; d < 8; d+= 2) {
131+
int nr = r + dr[d];
132+
int nc = c + dc[d];
133+
134+
// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๋ฒฝ & ๋‹ค๋ฅธ ๋‚˜๋ฌด๊ฐ€ ์žˆ๊ฑฐ๋‚˜, ์ œ์ดˆ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด continue
135+
if(!check(nr, nc) || map[nr][nc] != 0 || remove[nr][nc] != 0) continue;
136+
cnt++;
137+
}
138+
139+
for(int d = 1; d < 8; d+= 2) {
140+
int nr = r + dr[d];
141+
int nc = c + dc[d];
142+
143+
// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๋ฒฝ & ๋‹ค๋ฅธ ๋‚˜๋ฌด๊ฐ€ ์žˆ๊ฑฐ๋‚˜, ์ œ์ดˆ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด continue
144+
if(!check(nr, nc) || map[nr][nc] != 0 || remove[nr][nc] != 0) continue;
145+
tmp[nr][nc] += map[r][c] / cnt;
146+
}
147+
148+
}
149+
}
150+
151+
for(int r = 0; r < map.length; r++) {
152+
for(int c = 0; c < map[0].length; c++) {
153+
map[r][c] += tmp[r][c];
154+
}
155+
}
156+
}
157+
158+
// ์„ฑ์žฅ - ์ธ์ ‘ํ•œ ๋„ค ์นธ์˜ ์นธ ์ค‘ ๋‚˜๋ฌด๊ฐ€ ์žˆ๋Š” ์ˆ˜๋งŒํผ ๋‚˜๋ฌด ์„ฑ์žฅ
159+
static void grow() {
160+
161+
int[][] tmp = new int[N][N]; // ๋™์‹œ์— ์„ฑ์žฅํ•ด์•ผ ๋˜๋ฏ€๋กœ, tmp ๋ฐฐ์—ด์— ์ฆ๊ฐ์— ๋Œ€ํ•œ ์ •๋ณด ์ €์žฅ
162+
163+
for(int r = 0; r < map.length; r++) {
164+
for(int c = 0; c < map[0].length; c++) {
165+
if(map[r][c] <= 0) continue; // ๋‚˜๋ฌด๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด continue
166+
167+
int tree = 0;
168+
169+
// ์ธ์ ‘ํ•œ ๋„ค ์นธ
170+
for(int d = 1; d < 8; d += 2) {
171+
172+
int nr = r + dr[d];
173+
int nc = c + dc[d];
174+
175+
// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๋‚˜๋ฌด๊ฐ€ ์•„๋‹ ๋•Œ
176+
if(!check(nr, nc) || map[nr][nc] <= 0) continue;
177+
tree += 1;
178+
}
179+
180+
tmp[r][c] += tree;
181+
}
182+
}
183+
184+
for(int r = 0; r < map.length; r++) {
185+
for(int c = 0; c < map[0].length; c++) {
186+
map[r][c] += tmp[r][c];
187+
}
188+
}
189+
}
190+
191+
static boolean check(int r, int c) {
192+
return r >= 0 && r < map.length && c >= 0 && c < map[0].length;
193+
}
194+
195+
static void initInput() throws Exception {
196+
System.setIn(new FileInputStream("./input/๋‚˜๋ฌด๋ฐ•๋ฉธ.txt"));
197+
198+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
199+
StringTokenizer st = new StringTokenizer(br.readLine());
200+
201+
N = Integer.parseInt(st.nextToken()); // ๊ฒฉ์ž์˜ ํฌ๊ธฐ
202+
M = Integer.parseInt(st.nextToken()); // ๋ฐ•๋ฉธ์ด ์ง„ํ–‰๋˜๋Š” ๋…„ ์ˆ˜
203+
K = Integer.parseInt(st.nextToken()); // ์ œ์ดˆ์ œ์˜ ํ™•์‚ฐ ๋ฒ”์œ„
204+
C = Integer.parseInt(st.nextToken()); // ์ œ์ดˆ์ œ๊ฐ€ ๋‚จ์•„์žˆ๋Š” ๋…„์ˆ˜
205+
206+
map = new int[N][N]; // ๋‚˜๋ฌด
207+
remove = new int[N][N]; // ์ œ์ดˆ์ œ
208+
209+
for(int r = 0; r < map.length; r++) {
210+
st = new StringTokenizer(br.readLine());
211+
212+
for(int c = 0; c < map[0].length; c++) map[r][c] = Integer.parseInt(st.nextToken());
213+
}
214+
}
215+
}

0 commit comments

Comments
ย (0)