Submission #10184962


Source Code Expand

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
//負の数を掛けたりするとバグる
template<int MOD>
class Modint{
public:
    int a;
    Modint(const long long v = 0):a(v % MOD){}
    int getmod() const{
        return MOD;
    }
    Modint operator+(const Modint rhs) const{
        return Modint(*this) += rhs;
    }
    Modint operator-(const Modint rhs) const{
        return Modint(*this) -= rhs;
    }
    Modint operator*(const Modint rhs) const{
        return Modint(*this) *= rhs;
    }
    Modint operator/(const Modint rhs) const{
        return Modint(*this) /= rhs;
    }
    Modint operator+(const long long rhs) const{
        return Modint(*this) += rhs;
    }
    Modint operator-(const long long rhs) const{
        return Modint(*this) -= rhs;
    }
    Modint operator*(const long long rhs) const{
        return Modint(*this) *= rhs;
    }
    Modint operator/(const long long rhs) const{
        return Modint(*this) /= rhs;
    }
    friend Modint operator+(const long long a, const Modint b){
        return b + a;
    }
    friend Modint operator-(const long long a, const Modint b){
        return -b + a;
    }
    friend Modint operator*(const long long a, const Modint b){
        return b * a;
    }
    friend Modint operator/(const long long a, const Modint b){
        return Modint(a) / b;
    }
    Modint &operator+=(const Modint rhs){
        a += rhs.a;
        if(a >= MOD){
            a -= MOD;
        }
        return *this;
    }
    Modint &operator-=(const Modint rhs){
        if(a < rhs.a){
            a += MOD;
        }
        a -= rhs.a;
        return *this;
    }
    Modint &operator*=(const Modint rhs){
        a = (long long)a * rhs.a % MOD;
        return *this;
    }
    Modint &operator/=(Modint rhs){
        int x = MOD - 2;
        while(x){
            if(x % 2){
                *this *= rhs;
            }
            rhs *= rhs;
            x /= 2;
        }
        return *this;
    }
    Modint &operator++(){
        *this += 1;
        return *this;
    }
    Modint &operator--(){
        *this -= 1;
        return *this;
    }
    Modint operator++(int){
        Modint res = *this;
        ++(*this);
        return res;
    }
    Modint operator--(int){
        Modint res = *this;
        --(*this);
        return res;
    }
    Modint &operator+=(const long long rhs){
        *this += Modint(rhs);
        return *this;
    }
    Modint &operator-=(const long long rhs){
        *this -= Modint(rhs);
        return *this;
    }
    Modint &operator*=(const long long rhs){
        *this *= Modint(rhs);
        return *this;
    }
    Modint &operator/=(const long long rhs){
        *this /= Modint(rhs);
        return *this;
    }
    Modint operator+() const{
        return *this;
    }
    Modint operator-() const{
        return Modint()-*this;
    }
    bool operator==(const Modint rhs) const{
        return a == rhs.a;
    }
    bool operator==(const long long rhs) const{
        return a == rhs;
    }
    friend bool operator==(const long long a, const Modint b){
        return a == b.a;
    }
    bool operator!=(const Modint rhs) const{
        return a != rhs.a;
    }
    bool operator!=(const long long rhs) const{
        return a != rhs;
    }
    friend ostream &operator<<(ostream &os, const Modint x){
        os << x.a;
        return os;
    }
    friend istream &operator>>(istream &is, Modint &x){
        is >> x.a;
        return is;
    }
    explicit operator bool() const{
        return a > 0;
    }
    bool operator!(){
        return a == 0;
    }
    explicit operator int() const{
        return a;
    }
    explicit operator long long() const{
        return (long long) a;
    }
    friend Modint pow(Modint a, long long b){
        Modint res = 1;
        while(b){
            if(b % 2){
                res *= a;
            }
            a *= a;
            b /= 2;
        }
        return res;
    }
};
using mint = Modint<1000000007>;
const int NUM = 400005;
mint fact[NUM + 1], fact_inv[NUM + 1], inv[NUM + 1];
mint combi(long long N_, long long K_){
    static const int Mod_ = fact[0].getmod();
    if(fact[0] == 0){
        inv[1] = fact[0] = fact_inv[0] = 1;
        for(int i = 2; i <= NUM; i++){
            inv[i] = inv[Mod_ % i] * (Mod_ - Mod_ / i);
        }
        for(int i = 1; i <= NUM; i++){
            fact[i] = fact[i - 1] * i;
            fact_inv[i] = fact_inv[i - 1] * inv[i];
        }
    }
    if(K_ < 0 or K_ > N_) return 0;
    return fact_inv[K_] * fact[N_] * fact_inv[N_ - K_];
}
mint hcomb(long long N_, long long K_){
    return ((N_ | K_) == 0) ? 1 : combi(N_ + K_ - 1, K_);
}
int main(){
    int n, K;
    cin >> n >> K;
    vector<int>a(K);
    rep(i,K)cin >> a[i];
    vector<mint> dp(n + 1);
    dp[0] = 1;
    for(int i = 0; i < K; i++){
        vector<mint> ndp(n + 1);
        for(int j = 0; j <= n; j++){
            for(int x = 0; j + x <= n; x++){
                ndp[j+x] += dp[j] * combi(n - j, x) * combi(n - x, a[i] - x);
            }
        }
        swap(ndp, dp);
    }
    cout << dp[n] << endl;
}

Submission Info

Submission Time
Task C - Cookie Distribution
User polyomino
Language C++14 (GCC 5.4.1)
Score 800
Code Size 6413 Byte
Status AC
Exec Time 252 ms
Memory 4992 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 2
AC × 32
Set Name Test Cases
Sample sample_01, sample_02
All max_01, max_02, max_03, max_04, max_05, max_06, max_07, max_08, max_09, max_10, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_small_01, random_small_02, random_small_03, random_small_04, random_small_05, random_small_06, random_small_07, random_small_08, random_small_09, random_small_10, sample_01, sample_02
Case Name Status Exec Time Memory
max_01 AC 239 ms 4992 KB
max_02 AC 229 ms 4992 KB
max_03 AC 235 ms 4992 KB
max_04 AC 252 ms 4992 KB
max_05 AC 226 ms 4992 KB
max_06 AC 241 ms 4992 KB
max_07 AC 235 ms 4992 KB
max_08 AC 237 ms 4992 KB
max_09 AC 221 ms 4992 KB
max_10 AC 245 ms 4992 KB
random_01 AC 27 ms 4992 KB
random_02 AC 14 ms 4992 KB
random_03 AC 72 ms 4992 KB
random_04 AC 61 ms 4992 KB
random_05 AC 17 ms 4992 KB
random_06 AC 26 ms 4992 KB
random_07 AC 125 ms 4992 KB
random_08 AC 28 ms 4992 KB
random_09 AC 109 ms 4992 KB
random_10 AC 36 ms 4992 KB
random_small_01 AC 9 ms 4992 KB
random_small_02 AC 9 ms 4992 KB
random_small_03 AC 9 ms 4992 KB
random_small_04 AC 9 ms 4992 KB
random_small_05 AC 9 ms 4992 KB
random_small_06 AC 9 ms 4992 KB
random_small_07 AC 9 ms 4992 KB
random_small_08 AC 9 ms 4992 KB
random_small_09 AC 9 ms 4992 KB
random_small_10 AC 9 ms 4992 KB
sample_01 AC 9 ms 4992 KB
sample_02 AC 137 ms 4992 KB