結果

問題 No.1035 Color Box
ユーザー ks2mks2m
提出日時 2020-04-24 22:11:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,296 bytes
コンパイル時間 2,419 ms
コンパイル使用メモリ 77,608 KB
実行使用メモリ 56,532 KB
最終ジャッジ日時 2024-04-23 03:26:16
合計ジャッジ時間 9,365 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
56,168 KB
testcase_01 AC 137 ms
53,964 KB
testcase_02 AC 138 ms
54,404 KB
testcase_03 AC 142 ms
53,932 KB
testcase_04 AC 155 ms
56,144 KB
testcase_05 AC 143 ms
54,016 KB
testcase_06 AC 148 ms
54,184 KB
testcase_07 AC 149 ms
56,356 KB
testcase_08 AC 195 ms
56,500 KB
testcase_09 AC 166 ms
56,212 KB
testcase_10 AC 139 ms
54,072 KB
testcase_11 AC 139 ms
54,308 KB
testcase_12 AC 138 ms
54,140 KB
testcase_13 AC 190 ms
56,296 KB
testcase_14 AC 194 ms
56,052 KB
testcase_15 AC 197 ms
56,216 KB
testcase_16 AC 135 ms
53,896 KB
testcase_17 AC 137 ms
54,372 KB
testcase_18 AC 139 ms
54,388 KB
testcase_19 AC 201 ms
56,532 KB
testcase_20 AC 137 ms
54,408 KB
testcase_21 AC 134 ms
54,348 KB
testcase_22 AC 134 ms
54,184 KB
testcase_23 AC 140 ms
54,148 KB
testcase_24 AC 137 ms
53,984 KB
testcase_25 AC 139 ms
53,948 KB
testcase_26 AC 137 ms
54,096 KB
testcase_27 AC 137 ms
53,792 KB
testcase_28 AC 138 ms
54,088 KB
testcase_29 AC 138 ms
53,968 KB
testcase_30 AC 141 ms
54,204 KB
testcase_31 AC 136 ms
54,076 KB
testcase_32 AC 138 ms
54,124 KB
testcase_33 AC 123 ms
52,924 KB
testcase_34 AC 134 ms
54,052 KB
testcase_35 AC 136 ms
54,100 KB
testcase_36 AC 138 ms
54,056 KB
testcase_37 AC 138 ms
53,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		sc.close();

		int mod = 1000000007;
		NCR ncr = new NCR(n, mod);
		long ans = power(m, n, mod);
		boolean odd = true;
		for (int i = m - 1; i >- 1; i--) {
			long rem = power(i, n, mod) * ncr.calc(m, i) % mod;
			if (odd) {
				rem = -rem;
			}
			ans += rem;
			odd = !odd;
		}
		while (ans < 0) {
			ans += mod;
		}
		System.out.println(ans % mod);
	}

	static class NCR {
		long[] p, pi;
		int m;

		public NCR(int n, int mod) {
			n++;
			m = mod;
			p = new long[n];
			pi = new long[n];
			p[0] = 1;
			pi[0] = 1;
			for (int i = 1; i < n; i++) {
				p[i] = p[i - 1] * i % m;
			}
			pi[n - 1] = BigInteger.valueOf(p[n - 1])
					.modInverse(BigInteger.valueOf(m)).longValue();
			for (int i = n - 1; i > 1; i--) {
				pi[i - 1] = pi[i] * i % m;
			}
		}

		public long calc(int n, int r) {
			if (n < r) return 0;
			return p[n] * pi[r] % m * pi[n - r] % m;
		}
	}

	static long power(long x, long n, int m) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2, m);
		val = val * val % m;
		if (n % 2 == 1) {
			val = val * x % m;
		}
		return val;
	}
}
0