結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー ふーらくたる
提出日時 2016-07-09 00:06:38
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,263 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 60,892 KB
実行使用メモリ 7,404 KB
最終ジャッジ日時 2024-10-13 07:42:52
合計ジャッジ時間 3,060 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 99
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

#define int long long

const int kMOD = 1000 * 1000 * 1000 + 7;

const int kMAX_M = 1000010;

int M;
vector<int> H;

int factorial(int n) {
    int res = 1;
    for (int i = 0; i < n; i++) {
        res *= (n - i);
        res %= kMOD;
    }
    return res;
}

// a^b (mod p) を計算する
int ModPow(int a, int b, int p) {
    if (b == 0) return 1;
    else if (b % 2 == 0) {
        int d;
        d = ModPow(a, b / 2, p);
        return (d * d) % p;
    } else {
        return (a * ModPow(a, b - 1, p)) % p;
    }
}

void Solve() {
    int use = 0, free;
    if (H[0] == 0) {
        cout << 1 << endl;
        return;
    }
    for (int i = 0; i < H.size(); i++) {
        use += H[i];
    }
    free = M - use - (H.size() - 1);
    if (free < 0) {
        cout << "NA" << endl;
        return;
    }
    int ans = factorial(free + H.size());
    ans = (ans * ModPow(factorial(free), kMOD - 2, kMOD)) % kMOD;
    ans = (ans * ModPow(factorial(H.size()), kMOD - 2, kMOD)) % kMOD;

    cout << ans << endl;
}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> M;

    int h;
    while (cin >> h, !cin.eof()) {
        H.push_back(h);
    }

    Solve();

    return 0;
}
0