結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
41,280 KB
testcase_01 AC 124 ms
41,176 KB
testcase_02 AC 112 ms
39,904 KB
testcase_03 AC 113 ms
40,332 KB
testcase_04 AC 118 ms
40,772 KB
testcase_05 AC 125 ms
41,364 KB
testcase_06 AC 122 ms
41,248 KB
testcase_07 AC 128 ms
41,312 KB
testcase_08 AC 127 ms
41,376 KB
testcase_09 AC 216 ms
42,768 KB
testcase_10 AC 200 ms
42,308 KB
testcase_11 AC 204 ms
42,792 KB
testcase_12 AC 125 ms
41,436 KB
testcase_13 AC 202 ms
42,552 KB
testcase_14 AC 195 ms
42,532 KB
testcase_15 AC 199 ms
42,052 KB
testcase_16 AC 177 ms
42,280 KB
testcase_17 AC 177 ms
41,576 KB
testcase_18 AC 166 ms
41,404 KB
testcase_19 AC 165 ms
41,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No300台;

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;
		for (int i = 0; i <= m - 1; i++) {
			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