結果
問題 | No.391 CODING WAR |
ユーザー | a |
提出日時 | 2019-04-25 23:59:23 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 50 ms / 2,000 ms |
コード長 | 3,501 bytes |
コンパイル時間 | 1,592 ms |
コンパイル使用メモリ | 169,164 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-05-01 13:24:05 |
合計ジャッジ時間 | 2,825 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
6,812 KB |
testcase_01 | AC | 4 ms
6,940 KB |
testcase_02 | AC | 4 ms
6,944 KB |
testcase_03 | AC | 4 ms
6,940 KB |
testcase_04 | AC | 4 ms
6,948 KB |
testcase_05 | AC | 4 ms
6,940 KB |
testcase_06 | AC | 4 ms
6,944 KB |
testcase_07 | AC | 4 ms
6,944 KB |
testcase_08 | AC | 4 ms
6,944 KB |
testcase_09 | AC | 50 ms
6,944 KB |
testcase_10 | AC | 49 ms
6,940 KB |
testcase_11 | AC | 23 ms
6,944 KB |
testcase_12 | AC | 4 ms
6,944 KB |
testcase_13 | AC | 41 ms
6,940 KB |
testcase_14 | AC | 36 ms
6,944 KB |
testcase_15 | AC | 39 ms
6,944 KB |
testcase_16 | AC | 26 ms
6,944 KB |
testcase_17 | AC | 31 ms
6,944 KB |
testcase_18 | AC | 21 ms
6,940 KB |
testcase_19 | AC | 22 ms
6,940 KB |
ソースコード
#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; }