結果

問題 No.573 a^2[i] = a[i]
ユーザー startcppstartcpp
提出日時 2016-12-26 15:06:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,236 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 51,856 KB
実行使用メモリ 8,208 KB
最終ジャッジ日時 2023-08-21 12:42:22
合計ジャッジ時間 2,389 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,356 KB
testcase_01 AC 2 ms
5,432 KB
testcase_02 AC 3 ms
5,372 KB
testcase_03 AC 3 ms
5,356 KB
testcase_04 AC 2 ms
5,420 KB
testcase_05 AC 2 ms
5,360 KB
testcase_06 AC 2 ms
5,440 KB
testcase_07 AC 2 ms
5,432 KB
testcase_08 AC 2 ms
5,420 KB
testcase_09 AC 2 ms
5,500 KB
testcase_10 AC 2 ms
5,500 KB
testcase_11 AC 2 ms
5,644 KB
testcase_12 AC 2 ms
5,412 KB
testcase_13 AC 2 ms
5,396 KB
testcase_14 AC 2 ms
5,556 KB
testcase_15 AC 2 ms
5,420 KB
testcase_16 AC 2 ms
5,384 KB
testcase_17 AC 2 ms
5,416 KB
testcase_18 AC 2 ms
5,420 KB
testcase_19 AC 2 ms
5,396 KB
testcase_20 AC 2 ms
5,652 KB
testcase_21 AC 2 ms
5,648 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,548 KB
testcase_24 AC 2 ms
5,648 KB
testcase_25 AC 3 ms
5,668 KB
testcase_26 AC 2 ms
5,420 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,360 KB
testcase_29 AC 2 ms
5,448 KB
testcase_30 AC 3 ms
5,380 KB
testcase_31 AC 2 ms
5,648 KB
testcase_32 AC 2 ms
5,504 KB
testcase_33 AC 3 ms
5,404 KB
testcase_34 AC 3 ms
5,492 KB
testcase_35 AC 3 ms
5,556 KB
testcase_36 AC 4 ms
5,460 KB
testcase_37 AC 3 ms
5,388 KB
testcase_38 AC 4 ms
5,408 KB
testcase_39 AC 5 ms
5,388 KB
testcase_40 AC 5 ms
5,384 KB
testcase_41 AC 6 ms
5,560 KB
testcase_42 AC 7 ms
5,424 KB
testcase_43 AC 7 ms
5,432 KB
testcase_44 AC 10 ms
5,488 KB
testcase_45 AC 20 ms
5,432 KB
testcase_46 AC 195 ms
8,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#define int long long
using namespace std;

int mod = 1000000007;	//素数
int n;
int fact[1000001];		//fact[n] = n! % mod
int factInv[1000001];

//a^n % modを返す, 前処理は不要なのでmodは引数にしてよい。
int powmod(int a, int n, int mod) {
	if (n == 0) return 1;
	if (n % 2 == 0) return powmod((a * a) % mod, n / 2, mod) % mod;
	return (a * powmod(a, n - 1, mod)) % mod;
}

//nCk % modを返す (fact, factInvを前処理で求めてる必要あり。modが変わればfactも更新)
int comb(int n, int k) {
	if (k > n) return 0;
	int a = fact[n];
	int b = factInv[k];		//(k! * b) % mod = 1となる整数b. bはk!の逆元という。
	int c = factInv[n - k];	//任意の正整数x, y(x % y == 0)と素数pについて、(x / y) % p = (x * y^(p-2)) % pが成り立つことを利用している。(フェルマーの小定理)
	return (((a * b) % mod) * c) % mod;
}

signed main() {
	int i;
	
	cin >> n;
	fact[0] = 1;
	for (i = 1; i <= n; i++) fact[i] = (i * fact[i - 1]) % mod;
	for (i = 0; i <= n; i++) factInv[i] = powmod(fact[i], mod - 2, mod);
	
	int ans = 0;
	for (i = 1; i <= n; i++) {
		ans += comb(n, i) * powmod(i, n - i, mod);
		ans %= mod;
	}
	cout << ans << endl;
	return 0;
}
0