結果

問題 No.1035 Color Box
ユーザー rabimearabimea
提出日時 2023-02-15 19:38:24
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,124 bytes
コンパイル時間 2,477 ms
コンパイル使用メモリ 208,044 KB
実行使用メモリ 6,920 KB
最終ジャッジ日時 2023-09-04 19:51:41
合計ジャッジ時間 4,395 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
5,652 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 5 ms
5,592 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 5 ms
5,612 KB
testcase_08 AC 14 ms
5,636 KB
testcase_09 AC 9 ms
6,896 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 12 ms
6,128 KB
testcase_14 AC 13 ms
6,908 KB
testcase_15 AC 15 ms
6,920 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 16 ms
6,916 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

int readInt() {
	int x;
	readf!" %d"(x);
	return x;
}
int[] readArray(int n) {
	auto a = new int[](n);
	return a.map!(i => readInt).array;
}

immutable long P = (1e9 + 7).to!long;
struct Combinatorics(long P) {
	long[] inv, fac, ifac;
	this(int n) {
		inv = new long[](n + 1);
		fac = new long[](n + 1);
		ifac = new long[](n + 1);
		inv[1] = fac[0] = ifac[0] = 1;
		for(int i = 2; i <= n; i ++)
			inv[i] = (P - P / i) * inv[P % i] % P;
		for(int i = 1; i <= n; i ++) {
			fac[i] = i * fac[i - 1] % P;
			ifac[i] = inv[i] * ifac[i - 1] % P;
		}
	}
	long binom(int n, int m) {
		if(n < m || m < 0) return 0; // n >= 0
		return fac[n] * ifac[m] % P * ifac[n - m] % P;
	}
	long qpow(long a, long b) {
		long r = 1, t = a % P;
		for(; b; b /= 2) {
			if(b & 1)
				r = r * t % P;
			t = t * t % P;
		}
		return r;
	}
};

void main() {
	int n = readInt, m = readInt;
	
	auto comb = new Combinatorics!P(n);
	
	long ans = comb.qpow(m, n);
	foreach(i; 0 .. m) {
		int sgn = ((m - i) & 1) ? -1 : 1;
		ans += sgn * comb.qpow(i, n) * comb.binom(m, i);
		ans %= P;
	}
	
	ans %= P;
	if(ans < 0) ans += P;
	writeln(ans);
}

0