結果

問題 No.391 CODING WAR
ユーザー nanophoto12nanophoto12
提出日時 2020-06-28 22:47:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,640 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 203,192 KB
実行使用メモリ 4,740 KB
最終ジャッジ日時 2023-09-22 18:23:05
合計ジャッジ時間 3,128 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 24 ms
4,596 KB
testcase_10 AC 22 ms
4,592 KB
testcase_11 AC 13 ms
4,724 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 20 ms
4,740 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 19 ms
4,612 KB
testcase_16 AC 13 ms
4,376 KB
testcase_17 AC 14 ms
4,380 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 10 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

const ll mod = 1000000007;

ll mpow(ll x, ll n) {
	ll ans = 1; x %= mod;
	while (n != 0) {
		if (n & 1) ans = ans * x % mod;
		x = x * x % mod;
		n = n >> 1;
	}
	return ans;
}

ll inv_mod(ll a) { return mpow(a, mod - 2); }

class Factorial {
private:
	vector<ll> fac;
	vector<ll> ifac;
public:

	Factorial(ll N) {
		fac.push_back(1);
		for (int i = 0; i < N; i++) fac.push_back(fac[i] * (i + 1) % mod);
		ifac.resize(N + 1);
		ifac[N] = inv_mod(fac[N]);
		for (int i = 0; i < N; i++) ifac[N - 1 - i] = (ifac[N - i] * (N - i)) % mod;
	}

	ll fact(ll a) { return fac[a]; }
	ll ifact(ll a) { return ifac[a]; }

	ll cmb(ll a, ll b) {
		if (a == 0 && b == 0) return 1;
		if (a < b || a < 0 || b < 0) return 0;
		ll tmp = ifact(a - b) * ifact(b) % mod;
		return tmp * fac[a] % mod;
	}
	ll per(ll a, ll b) {
		if (a == 0 && b == 0) return 1;
		if (a < b || a < 0 || b < 0) return 0;
		return fac[a] * ifac[a - b] % mod;
	}
};

int main() {
	ll n, m;
	cin >> n >> m;
	ll all = mpow(m, n);
	Factorial f(m);
	for (int i = 1; i < m; i++) {
		ll elem = f.cmb(m, i) * mpow(m - i, n);
		elem %= mod;
		ll sign = (i % 2 == 1) ? -1 : 1;
		all += sign * elem + mod;
		all %= mod;
	}
	cout << all << endl;
	return 0;
}
0