結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー tsutajtsutaj
提出日時 2019-06-19 20:49:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 5,741 bytes
コンパイル時間 1,720 ms
コンパイル使用メモリ 103,192 KB
実行使用メモリ 34,472 KB
最終ジャッジ日時 2023-08-20 07:18:07
合計ジャッジ時間 9,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
34,292 KB
testcase_01 AC 36 ms
34,396 KB
testcase_02 AC 36 ms
34,228 KB
testcase_03 AC 35 ms
34,112 KB
testcase_04 AC 35 ms
34,284 KB
testcase_05 AC 35 ms
34,172 KB
testcase_06 AC 36 ms
34,112 KB
testcase_07 AC 34 ms
34,124 KB
testcase_08 AC 35 ms
34,276 KB
testcase_09 AC 40 ms
34,276 KB
testcase_10 AC 40 ms
34,128 KB
testcase_11 AC 43 ms
34,284 KB
testcase_12 AC 38 ms
34,220 KB
testcase_13 AC 57 ms
34,212 KB
testcase_14 AC 42 ms
34,124 KB
testcase_15 AC 38 ms
34,412 KB
testcase_16 AC 39 ms
34,236 KB
testcase_17 AC 43 ms
34,228 KB
testcase_18 AC 76 ms
34,288 KB
testcase_19 AC 36 ms
34,228 KB
testcase_20 AC 36 ms
34,272 KB
testcase_21 AC 34 ms
34,292 KB
testcase_22 AC 35 ms
34,108 KB
testcase_23 AC 35 ms
34,444 KB
testcase_24 AC 35 ms
34,204 KB
testcase_25 AC 36 ms
34,160 KB
testcase_26 AC 36 ms
34,220 KB
testcase_27 AC 36 ms
34,160 KB
testcase_28 AC 37 ms
34,268 KB
testcase_29 AC 34 ms
34,288 KB
testcase_30 AC 35 ms
34,200 KB
testcase_31 AC 36 ms
34,304 KB
testcase_32 AC 36 ms
34,284 KB
testcase_33 AC 35 ms
34,440 KB
testcase_34 AC 36 ms
34,276 KB
testcase_35 AC 37 ms
34,416 KB
testcase_36 AC 35 ms
34,412 KB
testcase_37 AC 35 ms
34,288 KB
testcase_38 AC 35 ms
34,352 KB
testcase_39 AC 37 ms
34,232 KB
testcase_40 AC 36 ms
34,288 KB
testcase_41 AC 36 ms
34,288 KB
testcase_42 AC 36 ms
34,160 KB
testcase_43 AC 36 ms
34,224 KB
testcase_44 AC 36 ms
34,232 KB
testcase_45 AC 36 ms
34,360 KB
testcase_46 AC 37 ms
34,236 KB
testcase_47 AC 35 ms
34,156 KB
testcase_48 AC 35 ms
34,172 KB
testcase_49 AC 80 ms
34,360 KB
testcase_50 AC 39 ms
34,172 KB
testcase_51 AC 39 ms
34,292 KB
testcase_52 AC 43 ms
34,292 KB
testcase_53 AC 39 ms
34,236 KB
testcase_54 AC 46 ms
34,320 KB
testcase_55 AC 35 ms
34,116 KB
testcase_56 AC 39 ms
34,112 KB
testcase_57 AC 54 ms
34,260 KB
testcase_58 AC 72 ms
34,288 KB
testcase_59 AC 62 ms
34,188 KB
testcase_60 AC 47 ms
34,220 KB
testcase_61 AC 94 ms
34,232 KB
testcase_62 AC 48 ms
34,236 KB
testcase_63 AC 71 ms
34,228 KB
testcase_64 AC 45 ms
34,284 KB
testcase_65 AC 40 ms
34,160 KB
testcase_66 AC 47 ms
34,112 KB
testcase_67 AC 45 ms
34,168 KB
testcase_68 AC 47 ms
34,432 KB
testcase_69 AC 36 ms
34,372 KB
testcase_70 AC 37 ms
34,444 KB
testcase_71 AC 40 ms
34,192 KB
testcase_72 AC 36 ms
34,424 KB
testcase_73 AC 34 ms
34,436 KB
testcase_74 AC 35 ms
34,112 KB
testcase_75 AC 36 ms
34,416 KB
testcase_76 AC 34 ms
34,412 KB
testcase_77 AC 36 ms
34,240 KB
testcase_78 AC 39 ms
34,416 KB
testcase_79 AC 36 ms
34,232 KB
testcase_80 AC 36 ms
34,284 KB
testcase_81 AC 35 ms
34,224 KB
testcase_82 AC 34 ms
34,288 KB
testcase_83 AC 36 ms
34,216 KB
testcase_84 AC 35 ms
34,172 KB
testcase_85 AC 35 ms
34,436 KB
testcase_86 AC 36 ms
34,472 KB
testcase_87 AC 35 ms
34,380 KB
testcase_88 AC 35 ms
34,224 KB
testcase_89 AC 50 ms
34,288 KB
testcase_90 AC 60 ms
34,284 KB
testcase_91 AC 77 ms
34,268 KB
testcase_92 AC 42 ms
34,156 KB
testcase_93 AC 61 ms
34,400 KB
testcase_94 AC 35 ms
34,284 KB
testcase_95 AC 61 ms
34,236 KB
testcase_96 AC 56 ms
34,112 KB
testcase_97 AC 56 ms
34,272 KB
testcase_98 AC 46 ms
34,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

// ModInt begin

using ll = long long;
template<ll mod>
struct ModInt {
    ll v;
    ll mod_pow(ll x, ll n) const {
        return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;
    }
    ModInt(ll a = 0) : v(a >= mod ? a % mod : a) {}
    ModInt operator+ ( const ModInt& b ) const {
        return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));
    }
    ModInt operator- () const {
        return ModInt(-v);
    }
    ModInt operator- ( const ModInt& b ) const {
        return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));
    }
    ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}
    ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}
    
    bool operator== ( const ModInt &b ) const {return v == b.v;}
    ModInt& operator+= ( const ModInt &b ) {
        v += b.v;
        if(v >= mod) v -= mod;
        return *this;
    }
    ModInt& operator-= ( const ModInt &b ) {
        v -= b.v;
        if(v < 0) v += mod;
        return *this;
    }
    ModInt& operator*= ( const ModInt &b ) {
        (v *= b.v) %= mod;
        return *this;
    }
    ModInt& operator/= ( const ModInt &b ) {
        (v *= mod_pow(b.v, mod-2)) %= mod;
        return *this;
    }
    ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }
    // operator int() const { return int(v); }
    // operator long long int() const { return v; }
};

template<ll mod>
ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
template<ll mod>
istream& operator>> (istream& in, ModInt<mod>& a) {
    in >> a.v;
    return in;
}

// ModInt end

// 各種組み合わせを求めるライブラリ
template <typename NumType>
struct Combination {
    int LIMIT;
    vector<NumType> fact_, finv_;

    Combination() {}
    Combination(int LIMIT_) : LIMIT(LIMIT_), fact_(LIMIT+1), finv_(LIMIT+1) {
        fact_[0] = finv_[LIMIT] = NumType(1);
        for(int i=1; i<=LIMIT; i++) {
            fact_[i] = fact_[i-1] * NumType(i);
        }
        
        finv_[LIMIT] /= fact_[LIMIT];
        for(int i=LIMIT-1; i>=0; i--) {
            finv_[i] = finv_[i+1] * NumType(i+1);
        }
    }

    inline NumType fact(int k) const { return fact_[k]; }
    inline NumType finv(int k) const { return finv_[k]; }
    NumType P(int n, int r) const {
        if(r < 0 or n < r) return NumType(0);
        return fact_[n] * finv_[n-r];
    }
    NumType C(int n, int r) const {
        if(r < 0 or n < r) return NumType(0);
        return fact_[n] * finv_[n-r] * finv_[r];
    }
    // 重複組み合わせ
    NumType H(int n, int r) const {
        if(n < 0 or r < 0) return NumType(0);
        return r == 0 ? NumType(1) : C(n + r - 1, r);
    }
    // ベル数 (区別できる n 個のボールを区別できない k 個以下の箱に分割)
    // B(n, n) := n 個のボールを任意個のグループに分割する場合の数
    NumType B(int n, int k) const {
        if(n == 0) return NumType(1);
        k = min(n, k);
        NumType ret(0);
        vector<NumType> pref(k + 1); pref[0] = NumType(1);
        for(int i=1; i<=k; i++) {
            if(i & 1) pref[i] = pref[i-1] - finv_[i];
            else pref[i] = pref[i-1] + finv_[i];
        }
        for(int i=1; i<=k; i++) {
            // 累乗が必要なので適宜書き換える?
            // ModInt 使うならこれでいい
            ret += NumType(i).pow(n) * finv_[i] * pref[k-i];
        }
        return ret;
    }
    // スターリング数 (区別できる n 個のボールを区別できない k 個の箱に分割)
    NumType S(int n, int k) const {
        if(n < k) return NumType(0);
        NumType ans(0);
        for(int i=0; i<=k; i++) {
            NumType val = C(k, i) * NumType(i).pow(n);
            if((k - i) % 2) ans -= val;
            else ans += val;
        }
        return ans * finv_[k];
    }
};

// P(n, k) := n の k 分割 (k 個の 0 以上の整数の和)
template <typename NumType, int LIMIT = 3010>
struct Partition {
    vector< vector<NumType> > dp;
    Partition() : dp(LIMIT, vector<NumType>(LIMIT)) {
        for(int k=0; k<LIMIT; k++) dp[0][k] = NumType(1);
        for(int i=1; i<LIMIT; i++) {
            for(int j=1; j<LIMIT; j++) {
                dp[i][j] += dp[i][j-1];
                if(i-j >= 0) dp[i][j] += dp[i-j][j];
            }
        }
    }
    inline NumType get(int n, int k) {
        if(n < 0 or k < 0) return NumType(0);
        return dp[n][k];
    }
};

using mint = ModInt<MOD>;

const int MAXN = 2000010;
int main() {
    int N, K = 0, h; cin >> N;
    int rem = N;
    while(cin >> h) rem -= h, K++;
    rem -= K - 1;

    Combination<mint> comb(MAXN);
    if(rem == N) cout << 1 << endl;
    else {
        if(K+1 < 0 or rem < 0) cout << "NA" << endl;
        else cout << comb.H(K+1, rem) << endl;
    }
    return 0;
}
0