結果

問題 No.44 DPなすごろく
ユーザー villach
提出日時 2017-10-11 09:54:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 155 ms / 5,000 ms
コード長 933 bytes
コンパイル時間 4,501 ms
コンパイル使用メモリ 77,444 KB
実行使用メモリ 58,760 KB
最終ジャッジ日時 2024-11-17 09:20:35
合計ジャッジ時間 9,192 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;

public class N44 {
	private int n;
	public long c;
	public long[][] dp;
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		N44 hoge = new N44(n);
		long i = hoge.solve(0, 0);
		System.out.println(i);
	}
	
	N44(int n){
		this.n = n;
		this.dp = new long[n][10000];
		for(int i = 0;i < n;++i){
			for(int j = 0;j < 10000;++j){
				dp[i][j] = -1;
			}
		}
	}
	
	public long solve(int step, int count){
		long a, b;
		if(step + 1 == n){
			return count + 1;
		}else if(step + 2 == n){
			return count + 2;
		}else if(step >= n){
			return 0;
		}
		if(dp[step + 1][count] != -1){
			a = dp[step + 1][count];
		}else{
			a = solve(step + 1, count);
			dp[step + 1][count] = a;
		}
		if(dp[step + 2][count] != -1){
			b = dp[step + 2][count];
		}else{
			b = solve(step + 2, count);
			dp[step + 2][count] = b;
		}
		return a + b;
	}
}
0