結果
問題 | No.1035 Color Box |
ユーザー |
![]() |
提出日時 | 2020-04-26 22:02:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 11 ms / 2,000 ms |
コード長 | 2,752 bytes |
コンパイル時間 | 1,725 ms |
コンパイル使用メモリ | 171,216 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-18 12:30:21 |
合計ジャッジ時間 | 2,775 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 36 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for (int i = 0; i < n; ++i)#define ALL(c) (c).begin(), (c).end()#define SUM(x) std::accumulate(ALL(x), 0LL)#define MIN(v) *std::min_element(v.begin(), v.end())#define MAX(v) *std::max_element(v.begin(), v.end())#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())using namespace std;typedef long long ll;template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }const int INF = 1e9;const long long INFL = 1LL<<60;const int mod = 1000000007;struct mint {ll x; // typedef long long ll;mint(ll x=0):x((x%mod+mod)%mod){}mint& operator+=(const mint a) {if ((x += a.x) >= mod) x -= mod;return *this;}mint& operator-=(const mint a) {if ((x += mod-a.x) >= mod) x -= mod;return *this;}mint& operator*=(const mint a) {(x *= a.x) %= mod;return *this;}mint operator+(const mint a) const {mint res(*this);return res+=a;}mint operator-(const mint a) const {mint res(*this);return res-=a;}mint operator*(const mint a) const {mint res(*this);return res*=a;}mint pow(ll t) const {if (!t) return 1;mint a = pow(t>>1);a *= a;if (t&1) a *= *this;return a;}// for prime modmint inv() const {return pow(mod-2);}mint& operator/=(const mint a) {return (*this) *= a.inv();}mint operator/(const mint a) const {mint res(*this);return res/=a;}};struct combination {vector<mint> fact, ifact; // 階乗と階乗の逆元combination(int n):fact(n+1),ifact(n+1) { // 階乗と階乗の逆元を n まで求めるassert(n < mod);fact[0] = 1;for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;ifact[n] = fact[n].inv();for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;}mint operator()(int n, int k) {if (k < 0 || k > n) return 0;return fact[n]*ifact[k]*ifact[n-k];}};// (x^n) % mod を求めるll mod_pow(ll x, ll n, ll mod) {ll res = 1;for (; n > 0; n >>=1, (x *= x) %= mod) if (n & 1) (res *= x) %= mod;return res;}// 包除原理 (Principle of inclusion-exclusion) を用いて以下の問題を解く// n 個の玉を区別する// k 個の箱を区別する// 各箱に入る玉の個数は1個以上ll calc_pie(ll n, ll k) {combination comb(k);mint ans(0);for (ll i = 0; i <= k; i++) {mint tmp = comb(k, i) * mod_pow(i, n, mod);if ((k - i) % 2 == 1) ans -= tmp;else ans += tmp;}return ans.x;}int main(){ll n, m; cin >> n >> m;cout << calc_pie(n, m) << endl;return 0;}