結果

問題 No.665 Bernoulli Bernoulli
ユーザー GrenacheGrenache
提出日時 2018-03-12 00:52:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,309 ms / 2,000 ms
コード長 2,665 bytes
コンパイル時間 3,002 ms
コンパイル使用メモリ 77,744 KB
実行使用メモリ 41,892 KB
最終ジャッジ日時 2024-04-23 14:22:06
合計ジャッジ時間 24,370 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
41,552 KB
testcase_01 AC 125 ms
40,840 KB
testcase_02 AC 1,309 ms
41,576 KB
testcase_03 AC 1,297 ms
41,664 KB
testcase_04 AC 1,237 ms
41,160 KB
testcase_05 AC 1,141 ms
40,944 KB
testcase_06 AC 1,131 ms
41,216 KB
testcase_07 AC 1,125 ms
41,892 KB
testcase_08 AC 1,101 ms
41,660 KB
testcase_09 AC 1,224 ms
41,712 KB
testcase_10 AC 1,102 ms
41,468 KB
testcase_11 AC 1,249 ms
41,616 KB
testcase_12 AC 1,232 ms
41,784 KB
testcase_13 AC 1,252 ms
41,132 KB
testcase_14 AC 1,262 ms
41,552 KB
testcase_15 AC 1,112 ms
41,736 KB
testcase_16 AC 1,190 ms
41,512 KB
testcase_17 AC 1,139 ms
41,556 KB
testcase_18 AC 1,101 ms
40,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder665 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		final int MOD = 1_000_000_007;

		long n = sc.nextLong();
		int nn = (int)(n % MOD);
		int k = sc.nextInt();

//		long stime = System.currentTimeMillis();

		PC pc = new PC(k + 1, MOD);

//		pr.println(System.currentTimeMillis() - stime);

		long[] b = new long[k + 1];
		b[0] = 1;
		for (int i = 1; i <= k; i++) {
			long tmp = 0;
			for (int j = 0; j < i; j++) {
				tmp += b[j] * pc.C(i + 1, j) % MOD;
				if (tmp >= MOD) {
					tmp -= MOD;
				}
//				tmp %= MOD;
			}
			b[i] = (int)((-tmp + MOD) % MOD * pc.fact[i] % MOD * pc.ifact[i + 1] % MOD);
		}

//		pr.println(System.currentTimeMillis() - stime);

		long tmp = 0;
		for (int i = 0; i <= k; i++) {
			tmp += (long)pc.C(k + 1, i) * pc.pow(nn + 1, k + 1 - i) % MOD * b[i] % MOD;
			tmp %= MOD;
		}
		tmp *= pc.fact[k] * pc.ifact[k + 1] % MOD;
		tmp %= MOD;

		pr.println(tmp);

//		pr.println(System.currentTimeMillis() - stime);
	}

    private static class PC {
    	// MOD must be prime number.
    	int MOD;
    	// fact[i] : i! % MOD
    	long[] fact;
    	// ifact[i] : 1/i! % MOD
    	long[] ifact;

    	PC(int size, int MOD) {
    		// O(size)
    		// n=sizeまでのnCrを求める。
    		// nHrはn+r-1Crになってしまうので注意

    		this.MOD = MOD;

    		fact = new long[size + 1];
    		fact[0] = 1;
    		for (int i = 1; i <= size; i++) {
    			fact[i] = fact[i - 1] * i % MOD;
    		}

    		ifact = new long[size + 1];

    		int loop = MOD - 2;
    		long x = fact[size];
    		ifact[size] = 1;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				ifact[size] = ifact[size] * x % MOD;
    			}
    			x = x * x % MOD;
    			loop /= 2;
    		}

    		for (int i = size - 1; i >= 0; i--) {
    			ifact[i] = ifact[i + 1] * (i + 1) % MOD;
    		}

    	}

    	// 組合せの数
    	long C(int n, int r) {
    		if (r > n) {
    			return 0;
    		}

    		return (((fact[n] * ifact[n - r]) % MOD) * ifact[r] % MOD);
    	}

    	long pow(int a, long n) {
    		long loop = n;
    		long ret = 1;
    		long x = a;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				ret = ret * x % MOD;
    			}
    			x = x * x % MOD;
    			loop /= 2;
    		}

    		return ret;
    	}
    }

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0