結果

問題 No.44 DPなすごろく
ユーザー YamaKasaYamaKasa
提出日時 2018-06-23 20:54:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 130 ms / 5,000 ms
コード長 1,060 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 73,540 KB
実行使用メモリ 56,108 KB
最終ジャッジ日時 2023-09-13 08:36:52
合計ジャッジ時間 5,899 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,744 KB
testcase_01 AC 117 ms
55,668 KB
testcase_02 AC 116 ms
55,876 KB
testcase_03 AC 118 ms
55,796 KB
testcase_04 AC 120 ms
55,936 KB
testcase_05 AC 125 ms
55,792 KB
testcase_06 AC 121 ms
55,620 KB
testcase_07 AC 120 ms
55,604 KB
testcase_08 AC 126 ms
55,580 KB
testcase_09 AC 120 ms
55,596 KB
testcase_10 AC 119 ms
55,972 KB
testcase_11 AC 123 ms
55,684 KB
testcase_12 AC 130 ms
55,436 KB
testcase_13 AC 119 ms
55,432 KB
testcase_14 AC 119 ms
55,856 KB
testcase_15 AC 119 ms
56,108 KB
testcase_16 AC 120 ms
55,676 KB
testcase_17 AC 120 ms
55,692 KB
testcase_18 AC 125 ms
55,740 KB
testcase_19 AC 122 ms
55,728 KB
testcase_20 AC 123 ms
55,840 KB
testcase_21 AC 121 ms
55,736 KB
testcase_22 AC 121 ms
55,764 KB
testcase_23 AC 125 ms
55,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		scan.close();
		long ans = 0;
		int n = N / 2;
		for(int i = 0; i <= n; i++) {
			int k = N - 2 * i;
			int n1 = k + i;
			ans += comb(n1, i);
		}
		System.out.println(ans);
	}
	// n >= m
	public static long comb(int n, int r) {
	    if (n - r < r) r = n - r;
	    if (r == 0) return 1;
	    if (r == 1) return n;

	    int[] num = new int[r];
	    int[] den = new int[r];

	    for (int k = 0; k < r; k++){
	        num[k] = n - r + k + 1;
	        den[k] = k + 1;
	    }

	    for (int p = 2; p <= r; p++) {
	        int pivot = den[p - 1];
	        if (pivot > 1) {
	            int offset = (n - r) % p;
	            for (int k = p - 1; k < r; k += p) {
	                num[k - offset] /= pivot;
	                den[k] /= pivot;
	            }
	        }
	    }

	    long result = 1;
	    for (int k = 0; k < r; k++) {
	        if (num[k] > 1) result *= num[k];
	    }

	    return result;
	}

}
0