結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー ctyl_0ctyl_0
提出日時 2016-07-08 22:49:06
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,671 bytes
コンパイル時間 627 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 7,520 KB
最終ジャッジ日時 2024-10-13 06:15:17
合計ジャッジ時間 3,331 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 99
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template
ll pow_mod(ll a, ll b, ll m){
    ll ret = 1;
    while (b > 0) {
        if (b & 1) {
            ret = ret * a % m;
        }
        a = a * a % m;
        b >>= 1;
    }
    return ret;
}
ll inv_mod(ll a, ll m){
    return pow_mod(a, m - 2, m);
}

ll fact(ll n, ll m){
    ll ret = 1;
    repd(i, 1, n + 1){
        (ret *= i) %= m;
    }
    return ret;
}

ll comb(ll n, ll k, ll m){
    ll fn = fact(n, m);
    ll fk = fact(k, m);
    ll fnk = fact(n - k, m);
    ll b = inv_mod(fk * fnk % m, m);
    return fn * b % m;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    ll M;
    cin >> M;
    vector<ll> H;
    ll sum = 0;
    ll cnt = 0;
    ll h;
    while(cin >> h){
        sum += h;
        H.pb(h);
        cnt++;
    }
    
    if(sum + cnt - 1 > M){
        output("NA", 0);
        return 0;
    }
    
    if(H[0] == 0){
        output(1, 0);
        return 0;
    }
    
    ll ret = comb(M - sum + 1, cnt, mod);
    output((ret + mod) % mod, 0);
    
    return 0;
}
0