結果
問題 | No.389 ロジックパズルの組み合わせ |
ユーザー |
|
提出日時 | 2016-07-08 22:38:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 103 ms / 2,000 ms |
コード長 | 1,554 bytes |
コンパイル時間 | 501 ms |
コンパイル使用メモリ | 56,776 KB |
実行使用メモリ | 14,056 KB |
最終ジャッジ日時 | 2024-10-13 04:37:03 |
合計ジャッジ時間 | 4,097 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 99 |
ソースコード
#include <cstdio> #include <vector> #include <algorithm> #include <numeric> #include <cassert> #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) #define whole(f,x,...) ([&](decltype((x)) y) { return (f)(begin(y), end(y), ## __VA_ARGS__); })(x) typedef long long ll; using namespace std; const ll mod = 1e9+7; ll powi(ll x, ll y, ll p) { assert (y >= 0); x = (x % p + p) % p; ll z = 1; for (ll i = 1; i <= y; i <<= 1) { if (y & i) z = z * x % p; x = x * x % p; } return z; } ll inv(ll x, ll p) { assert ((x % p + p) % p != 0); return powi(x, p-2, p); } ll choose(ll n, ll r) { // O(n) at first time, otherwise O(1) static vector<ll> fact(1,1); static vector<ll> ifact(1,1); if (fact.size() <= n) { int l = fact.size(); fact.resize( n + 1); ifact.resize(n + 1); repeat_from (i,l,n+1) { fact[i] = fact[i-1] * i % mod; ifact[i] = inv(fact[i], mod); } } r = min(r, n - r); return fact[n] * ifact[n-r] % mod * ifact[r] % mod; } int main() { int m; scanf("%d", &m); vector<int> h; for (int t; scanf("%d", &t) != EOF;) h.push_back(t); int sum = whole(accumulate, h, 0); int blank = m - (sum + h.size()-1); if (h.size() == 1 and h[0] == 0) { printf("1\n"); } else if (blank >= 0) { int ans = choose(blank + h.size(), h.size()); printf("%d\n", ans); } else { printf("NA\n"); } return 0; }