結果

問題 No.44 DPなすごろく
ユーザー YamaKasa
提出日時 2018-06-23 20:54:16
言語 Java
(openjdk 23)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,060 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 77,224 KB
実行使用メモリ 54,260 KB
最終ジャッジ日時 2024-06-30 18:33:51
合計ジャッジ時間 6,182 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

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