π [λ°±μ€μκ³ λ¦¬μ¦ νμ΄] 10844 μ¬μ΄ κ³λ¨ μ
π λ¬Έμ
https://www.acmicpc.net/problem/10844
π μ κ·Όλ²
κΈΈμ΄κ° NμΈ κ³λ¨ μκ° μ΄ λͺ κ°μΈμ§ ꡬνλ λ¬Έμ μ΄λ€.
μΈμ ν λͺ¨λ μ리μμ μ°¨μ΄κ° 1μΈ κ³λ¨ μλ₯Ό ꡬνλ λ¬Έμ μ΄λ€.
κ³λ¨ μλ λ€μ μκ° 1μ΄ ν¬κ±°λ 1μ΄ μμΌλ©΄ λλ€.
n=1 1 2 3 . . . 9
n=2 12 10 21 23 34 32 . . . 98 9 λ€μ μ«μλ‘λ μ»€μ§ μ μμ!
λ¨
μ«μμ λ²μκ° 0~9 μ΄λ―λ‘ μ«μκ° 0μΌλλ
λ€μ μ«μκ° 1μ΄ λ μ€μ΄λ€ μ μκ³
μ«μκ° 9μΌ λλ λ€μ μ«μκ° 1μ΄ λ μ»€μ§ μ μλ€.
π» μ½λ
package problem.dynamic.Q10844;
import java.io.*;
public class Main_10844 {
public static int mod = 1000000000;
public static int n;
public static int[][] d;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
d = new int[n+1][10];
long answer = solve(n);
bw.write(Long.toString(answer));
bw.flush();
bw.close();
}
public static long solve(int n){
long answer = 0;
for(int i=1; i<=9; i++){
d[1][i] = 1; //1κ° μ§λ¦¬ κ³λ¨μ 1/2/3/4/5/6/7/8/9
}
for(int i=2; i<=n; i++) {
for(int j=0; j<=9; j++){
if(j+1 <= 9){
d[i][j] += d[i-1][j+1];
}
if(j-1 >= 0){
d[i][j] += d[i-1][j-1];
}
d[i][j] %= mod;
}
}
for(int i=0; i<=9; i++){
answer += d[n][i];
}
return answer%mod;
}
}
Written on April 23, 2020