๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Problem Solving

[Java] ๋ฐฑ์ค€ 31443 - ์ค€์˜์ด

https://www.acmicpc.net/problem/31443


ํ’€์ด

ํฌ๊ธฐ๊ฐ€ 3์ธ ์กฐ๊ฐ์„ ์ตœ๋Œ€๋งŒ ๋งŽ์ด ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

์˜ˆ์™ธ์ ์œผ๋กœ ํฌ๊ธฐ๊ฐ€ 4์ธ ์กฐ๊ฐ์€ 2×2๋กœ ์ชผ๊ฐœ๋Š” ๊ฒŒ ๋” ์ด๋“์ด๋‹ค.


์ฆ๋ช…

(์‹์ด ๊ธธ์–ด์ ธ ๋ฏธ๋ถ„ ๊ณผ์ •์€ ์ƒ๋žต)


์•„๋ฌดํŠผ ์ดˆ์ฝ”๋ฐ”๋ฅผ ์ผ์ •ํ•œ ํฌ๊ธฐ์˜ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ์ดˆ์ฝ”๋ฐ”๊ฐ€ ํ•ด๋‹น ํฌ๊ธฐ๋กœ ๋”ฑ ์•Œ๋งž๊ฒŒ ๋‚˜๋ˆ ์ง„๋‹ค๋Š” ๊ฐ€์ •ํ•˜์—๋Š” ์กฐ๊ฐ์˜ ํฌ๊ธฐ๊ฐ€ e = 2.718281828 ์ผ ๋•Œ ๊ธฐ์จ์˜ ์ˆ˜์น˜๊ฐ€ ์ตœ๋Œ€๊ฐ€ ๋œ๋‹ค. ์ด ๊ฐ’์€ ๋ฐ˜์˜ฌ๋ฆผํ•˜๋ฉด 3์ด๋‹ˆ๊นŒ ๋Œ€๊ฐ• 3์œผ๋กœ ๋‚˜๋ˆ ๋ณด์ž.


์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.*;
import java.util.*;

public class Main {
	public static final int MOD = 1000000007;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		long answer = 1;
		int size = N * M;

		while (size > 4) {
			answer *= 3;
			answer %= MOD;
			size -= 3;
		}
		answer *= size;
		answer %= MOD;

		System.out.println(answer);
		br.close();
	}
}

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค

๋”๋ณด๊ธฐ
case #1:
5 6
59049

case #2:
5 7
354294

case #3:
5 8
2125764

case #4:
7 8
774840978

case #5:
1 1
1

ํ›„๊ธฐ

์กฐ๊ทธ๋งŒ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ดˆ์ฝ”๋ฐ”๋ฅผ ์ตœ๋Œ€ํ•œ ์ž˜๊ฒŒ ์ž๋ฅด๋ฉด ๋˜๊ฒ ๋‹ค๋Š” ๊ฐ์ด ์˜จ๋‹ค.

(โˆต ์–ด๋–ค ์ˆ˜ xª๋ฅผ ํฌ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” a๋ฅผ ํฌ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒŒ ์ด๋“์ด๋‹ˆ๊นŒ)

๊ทผ๋ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๋งŒ๋“ค๋‹ค ๋ณด๋‹ˆ ์˜ˆ์ƒ์™ธ๋กœ 2๋ณด๋‹ค๋Š” 3์ด ๋” ์ด๋“์ด๋ผ ๋‹นํ™ฉํ–ˆ๋˜ ๋ฌธ์ œ..

๊ทธ๋ฆฌ๋””๋Š” ๋งค๋ฒˆ ํ˜ผ๋ž€์Šค๋Ÿฝ๋‹ค..