結果

問題 No.1035 Color Box
ユーザー ks2mks2m
提出日時 2020-04-24 22:11:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,296 bytes
コンパイル時間 4,012 ms
コンパイル使用メモリ 74,144 KB
実行使用メモリ 58,444 KB
最終ジャッジ日時 2023-08-05 05:09:41
合計ジャッジ時間 8,886 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 186 ms
58,136 KB
testcase_01 AC 123 ms
56,144 KB
testcase_02 AC 122 ms
56,016 KB
testcase_03 AC 128 ms
56,236 KB
testcase_04 AC 140 ms
58,288 KB
testcase_05 AC 127 ms
56,420 KB
testcase_06 AC 126 ms
56,340 KB
testcase_07 AC 131 ms
58,432 KB
testcase_08 AC 173 ms
57,868 KB
testcase_09 AC 150 ms
58,444 KB
testcase_10 AC 125 ms
55,868 KB
testcase_11 AC 123 ms
55,744 KB
testcase_12 AC 122 ms
55,900 KB
testcase_13 AC 170 ms
58,228 KB
testcase_14 AC 176 ms
58,416 KB
testcase_15 AC 184 ms
58,300 KB
testcase_16 AC 124 ms
55,568 KB
testcase_17 AC 121 ms
55,880 KB
testcase_18 AC 123 ms
55,852 KB
testcase_19 AC 182 ms
57,628 KB
testcase_20 AC 120 ms
55,912 KB
testcase_21 AC 122 ms
56,360 KB
testcase_22 AC 122 ms
56,116 KB
testcase_23 AC 123 ms
54,080 KB
testcase_24 AC 121 ms
56,016 KB
testcase_25 AC 120 ms
56,288 KB
testcase_26 AC 121 ms
55,848 KB
testcase_27 AC 120 ms
56,572 KB
testcase_28 AC 119 ms
56,172 KB
testcase_29 AC 120 ms
56,064 KB
testcase_30 AC 118 ms
56,340 KB
testcase_31 AC 119 ms
57,956 KB
testcase_32 AC 122 ms
55,600 KB
testcase_33 AC 120 ms
56,500 KB
testcase_34 AC 119 ms
56,400 KB
testcase_35 AC 122 ms
55,956 KB
testcase_36 AC 120 ms
56,064 KB
testcase_37 AC 118 ms
56,664 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