結果

問題 No.391 CODING WAR
ユーザー aa
提出日時 2019-04-25 23:59:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 3,501 bytes
コンパイル時間 1,499 ms
コンパイル使用メモリ 166,656 KB
実行使用メモリ 5,112 KB
最終ジャッジ日時 2023-08-13 23:50:39
合計ジャッジ時間 2,768 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,936 KB
testcase_01 AC 3 ms
4,992 KB
testcase_02 AC 3 ms
5,112 KB
testcase_03 AC 4 ms
4,984 KB
testcase_04 AC 3 ms
5,108 KB
testcase_05 AC 3 ms
5,044 KB
testcase_06 AC 3 ms
5,044 KB
testcase_07 AC 3 ms
4,924 KB
testcase_08 AC 3 ms
4,924 KB
testcase_09 AC 45 ms
5,108 KB
testcase_10 AC 44 ms
4,940 KB
testcase_11 AC 20 ms
4,924 KB
testcase_12 AC 3 ms
4,972 KB
testcase_13 AC 38 ms
5,040 KB
testcase_14 AC 33 ms
4,984 KB
testcase_15 AC 38 ms
4,968 KB
testcase_16 AC 24 ms
4,912 KB
testcase_17 AC 29 ms
4,924 KB
testcase_18 AC 19 ms
4,924 KB
testcase_19 AC 21 ms
4,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define rep(n, i) Rep(0, n - 1, i)
#define INF 10000000
#define MAX 100000

const int MOD = 1e9 + 7;

typedef long long ll;

//階乗とその逆元
ll fac[MAX + 1], facInv[MAX + 1];

ll power(ll e, ll x)
{ //e^x % MOD
	if (x == 0)
		return 1LL;
	if (x % 2 != 0)
		return ((power(e, x - 1) * e) % MOD);
	ll temp = power(e, x / 2);
	return (temp * temp) % MOD;
}

ll nck(ll n, ll k)
{
	if (!(n >= k && k >= 0))
		return 0;
	ll temp = (fac[n] * facInv[n - k]) % MOD;
	return ((temp * facInv[k]) % MOD);
}

void fact(void)
{
	//階乗とその逆元
	fac[0] = facInv[0] = 1; //0! = 1
	//(x!)^(-1) ≡ (x!)^(p-2) (mod p)
	Rep(1, MAX, i) fac[i] = (fac[i - 1] * i) % MOD;
	facInv[MAX] = power(fac[MAX], MOD - 2);
	Rep(1, MAX - 1, i) facInv[MAX - i] = (facInv[MAX - i + 1] * (MAX - i + 1)) % MOD;
}

template <int p>
struct Modint
{
	int value;

	Modint() : value(0) {}
	Modint(long x) : value(x >= 0 ? x % p : (p + x % p) % p) {}

	inline Modint &operator+=(const Modint &b)
	{
		if ((this->value += b.value) >= p)
			this->value -= p;
		return (*this);
	}
	inline Modint &operator-=(const Modint &b)
	{
		if ((this->value += p - b.value) >= p)
			this->value -= p;
		return (*this);
	}
	inline Modint &operator*=(const Modint &b)
	{
		this->value = (int)((1LL * this->value * b.value) % p);
		return (*this);
	}
	inline Modint &operator/=(const Modint &b)
	{
		(*this) *= b.inverse();
		return (*this);
	}

	Modint operator+(const Modint &b) const { return Modint(*this) += b; }
	Modint operator-(const Modint &b) const { return Modint(*this) -= b; }
	Modint operator*(const Modint &b) const { return Modint(*this) *= b; }
	Modint operator/(const Modint &b) const { return Modint(*this) /= b; }

	inline Modint &operator++(int) { return (*this) += 1; }
	inline Modint &operator--(int) { return (*this) -= 1; }

	inline bool operator==(const Modint &b) const { return this->value == b.value; }
	inline bool operator!=(const Modint &b) const { return this->value != b.value; }
	inline bool operator<(const Modint &b) const { return this->value < b.value; }
	inline bool operator<=(const Modint &b) const { return this->value <= b.value; }
	inline bool operator>(const Modint &b) const { return this->value > b.value; }
	inline bool operator>=(const Modint &b) const { return this->value >= b.value; }

	//requires that "this->value and p are co-prime"
	// a_i * v + a_(i+1) * p = r_i
	// r_i = r_(i+1) * q_(i+1) * r_(i+2)
	// q == 1 (i > 1)
	// reference: https://atcoder.jp/contests/agc026/submissions/2845729 (line:93)
	inline Modint inverse() const
	{
		assert(this->value != 0);
		int r0 = p, r1 = this->value, a0 = 0, a1 = 1;
		while (r1)
		{
			int q = r0 / r1;
			r0 -= q * r1;
			swap(r0, r1);
			a0 -= q * a1;
			swap(a0, a1);
		}
		return Modint(a0);
	}

	friend istream &operator>>(istream &is, Modint<p> &a)
	{
		long t;
		is >> t;
		a = Modint<p>(t);
		return is;
	}
	friend ostream &operator<<(ostream &os, const Modint<p> &a)
	{
		return os << a.value;
	}
};

using Int = Modint<MOD>;

Int pow(Int e, ll x)
{
	if (x == 0)
		return 1;
	Int res = (x & 1 ? e : 1), tmp = pow(e, x >> 1);
	res *= tmp * tmp;
	return res;
}

void solve(void)
{
	long n, m;
	cin >> n >> m;
	Int ans = 0;
	fact();
	for (int i = m; i > 0; i--)
	{
		ans += Int(abs(i - m) & 1 ? -1 : 1) * pow(Int(i), n) * Int(nck(m, i));
	}
	cout << ans << endl;
}

int main()
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}
0