結果

問題 No.391 CODING WAR
ユーザー 37zigen37zigen
提出日時 2016-07-08 02:13:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 1,582 bytes
コンパイル時間 2,446 ms
コンパイル使用メモリ 77,616 KB
実行使用メモリ 42,928 KB
最終ジャッジ日時 2024-04-21 01:04:34
合計ジャッジ時間 6,451 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
40,088 KB
testcase_01 AC 116 ms
40,220 KB
testcase_02 AC 117 ms
40,208 KB
testcase_03 AC 126 ms
41,464 KB
testcase_04 AC 128 ms
41,596 KB
testcase_05 AC 130 ms
41,420 KB
testcase_06 AC 129 ms
41,308 KB
testcase_07 AC 128 ms
41,380 KB
testcase_08 AC 128 ms
41,276 KB
testcase_09 AC 209 ms
42,184 KB
testcase_10 AC 206 ms
42,656 KB
testcase_11 AC 204 ms
42,928 KB
testcase_12 AC 113 ms
40,040 KB
testcase_13 AC 202 ms
42,408 KB
testcase_14 AC 195 ms
41,780 KB
testcase_15 AC 200 ms
42,136 KB
testcase_16 AC 168 ms
41,156 KB
testcase_17 AC 192 ms
42,168 KB
testcase_18 AC 165 ms
40,792 KB
testcase_19 AC 175 ms
42,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		solver();
	}

	static final long MOD = 1_000_000_007;
	static long[] fact;
	static long[] inv_fact;

	static void solver() {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		int m = sc.nextInt();
		fact = new long[m + 1];
		inv_fact = new long[m + 1];
		fact[0] = 1;
		fact[1] = 1;
		for (int i = 2; i <= m; i++) {
			fact[i] = fact[i - 1] * (i % MOD) % MOD;
		}
		long ans = 0;
		ans += pow(m, n);
		for (int i = 1; i <= m - 1; i++) {
			ans = ans + nCk(m, i) * pow(m - i, n) % MOD * (i % 2 == 0 ? 1 : -1);
			if (ans < 0)
				ans += MOD;
			ans %= MOD;
		}
		System.out.println(ans);

	}

	static long nCk(int n, int k) {
		if (n < k)
			return 0;
		else {
			if (inv_fact[n - k] == 0)
				inv_fact[n - k] = inv(fact[n - k], MOD);
			if (inv_fact[k] == 0)
				inv_fact[k] = inv(fact[k], MOD);
			return fact[n] * inv_fact[n - k] % MOD * inv_fact[k] % MOD;
		}
	}

	static long fact(int n) {
		long ans = 1;
		for (int i = 1; i <= n; i++)
			ans *= i;
		return ans;
	}

	static long pow(long a, long n) {
		long A = a;
		long ans = 1;
		while (n >= 1) {
			if (n % 2 == 0) {
				A = (A * A) % MOD;
				n /= 2;
			} else if (n % 2 == 1) {
				ans = ans * A % MOD;
				n--;
			}
		}
		return ans;
	}

	static long inv(long a, long mod) {
		a = a % mod;
		long b = mod;
		long p = 1, q = 0;
		while (b > 1) {
			long c = b / a;
			b = b % a;
			q = q - p * c;
			long d = b;
			b = a;
			a = d;
			d = p;
			p = q;
			q = d;
		}
		while (q < 0)
			q += mod;
		return q;
	}
}
0