結果
| 問題 |
No.1035 Color Box
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-19 19:58:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 18 ms / 2,000 ms |
| コード長 | 3,942 bytes |
| コンパイル時間 | 749 ms |
| コンパイル使用メモリ | 76,244 KB |
| 実行使用メモリ | 5,632 KB |
| 最終ジャッジ日時 | 2024-10-06 04:52:19 |
| 合計ジャッジ時間 | 2,174 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 36 |
ソースコード
#include <iostream>
#include <vector>
#include <map>
using namespace std;
constexpr long long MOD = 1000000007;
//Combination Mod
class CombinationMod {
public:
vector<long long> fac,finv,inv;
long long mod;
CombinationMod(int N,long long mod) : fac(N + 1), finv(N + 1), inv(N + 1), mod(mod) {
fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1;
for (int i = 2; i <= N; ++i) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod%i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
long long num(int n, int k) {
return ((n < 0 || k < 0 || n < k) ? 0 : fac[n] * (finv[k] * finv[n - k] % mod) % mod);
}
};
//Pow_Mod O(log(n))
long long PowMod(long long x, long long n, long long mod) {
long long res = 1;
for (; n > 0; n >>= 1, (x *= x) %= mod) if (n & 1) (res *= x) %= mod;
return res;
}
template<long long mod> class ModInt {
public:
long long x;
ModInt():x(0) {
// do nothing
}
ModInt(long long y) : x(y>=0?(y%mod): (mod - (-y)%mod)%mod) {
// do nothing
}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator+=(const long long y) {
ModInt p(y);
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator+=(const int y) {
ModInt p(y);
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const long long y) {
ModInt p(y);
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const int y) {
ModInt p(y);
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (x * p.x % mod);
return *this;
}
ModInt &operator*=(const long long y) {
ModInt p(y);
x = (x * p.x % mod);
return *this;
}
ModInt &operator*=(const int y) {
ModInt p(y);
x = (x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inv();
return *this;
}
ModInt &operator/=(const long long y) {
ModInt p(y);
*this *= p.inv();
return *this;
}
ModInt &operator/=(const int y) {
ModInt p(y);
*this *= p.inv();
return *this;
}
ModInt operator=(const int y) {
ModInt p(y);
*this = p;
return *this;
}
ModInt operator=(const long long y) {
ModInt p(y);
*this = p;
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator++() {
x++;
if(x>=mod) x-=mod;
return *this;
}
ModInt operator--() {
x--;
if(x<0) x+=mod;
return *this;
}
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inv() const {
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(long long n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
long long t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
};
using modint = ModInt<MOD>;
int main(void) {
int N,M; cin >> N >> M;
CombinationMod CM(M,MOD);
int sgn = 1;
modint ans = 0;
for(int i = M; 1 <= i; --i) {
modint cnt = sgn;
cnt *= CM.num(M,i);
cnt *= PowMod(i,N,MOD);
ans += cnt;
sgn *= -1;
}
cout << ans << endl;
return 0;
}