結果

問題 No.503 配列コレクション
ユーザー 37zigen37zigen
提出日時 2017-04-08 03:15:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 1,496 bytes
コンパイル時間 5,010 ms
コンパイル使用メモリ 74,848 KB
実行使用メモリ 103,740 KB
最終ジャッジ日時 2023-09-23 04:19:56
合計ジャッジ時間 11,271 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 273 ms
103,628 KB
testcase_01 AC 290 ms
103,452 KB
testcase_02 AC 297 ms
103,528 KB
testcase_03 AC 292 ms
103,528 KB
testcase_04 AC 296 ms
103,392 KB
testcase_05 AC 299 ms
103,528 KB
testcase_06 AC 297 ms
103,492 KB
testcase_07 AC 286 ms
103,532 KB
testcase_08 AC 302 ms
103,256 KB
testcase_09 AC 309 ms
103,740 KB
testcase_10 AC 308 ms
103,256 KB
testcase_11 AC 299 ms
103,492 KB
testcase_12 AC 305 ms
103,272 KB
testcase_13 AC 296 ms
103,576 KB
testcase_14 AC 300 ms
103,736 KB
testcase_15 AC 309 ms
103,288 KB
testcase_16 AC 306 ms
103,340 KB
testcase_17 AC 318 ms
103,464 KB
testcase_18 AC 311 ms
103,340 KB
testcase_19 AC 317 ms
103,296 KB
testcase_20 AC 314 ms
103,620 KB
testcase_21 AC 304 ms
103,408 KB
testcase_22 AC 306 ms
103,284 KB
testcase_23 AC 302 ms
103,256 KB
testcase_24 AC 309 ms
103,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	final long mo = 1_000_000_000 + 7;

	void run() {
		long[] fac = new long[2_000_000];
		long[] invfac = new long[fac.length];
		long[] inv = new long[fac.length];
		fac[0] = 1;
		inv[0] = 1;
		inv[1] = 1;
		invfac[0] = 1;
		for (int i = 1; i < fac.length; ++i) {
			fac[i] = fac[i - 1] * i % mo;
		}
		for (int i = 2; i < inv.length; ++i) {
			// 0=p/i*i+p%i
			// i^(-1)=-1*(p/i)*(p%i)^(-1)
			inv[i] = (mo - inv[(int) (mo % i)] * (mo / i) % mo) % mo;
		}
		for (int i = 1; i < invfac.length; ++i) {
			invfac[i] = invfac[i - 1] * inv[i] % mo;
		}
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int D = sc.nextInt();
		int q = N / (K - 1);
		if (N % (K - 1) == 0)
			--q;
		int res = N - q * (K - 1);
		if (D == 1) {
			System.out.println(res);
			return;
		}
		long comb = 0;
		long pow = 1;
		for (int i = 0; i <= q; ++i) {
			comb = comb + pow * H(q - i, res - 1, fac, invfac, mo);
			comb %= mo;
			pow = (pow * D) % mo;
		}
		comb = (comb * res) % mo;
		System.out.println(comb);
	}

	long H(int a, int b, long[] fac, long[] invfac, long mo) {
		// (a+b-1)C(a)
		if (a > 0 && b == 0)
			return 0;
		if (a == 0 && b == 0)
			return 1;
		return fac[a + b - 1] * invfac[a] % mo * invfac[b - 1] % mo;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0