結果

問題 No.584 赤、緑、青の色塗り
ユーザー imulanimulan
提出日時 2018-01-12 04:45:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 2,305 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 167,340 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-25 12:37:19
合計ジャッジ時間 2,956 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 46 ms
4,376 KB
testcase_17 AC 44 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 160 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const ll mod = 1000000007;

ll mod_pow(ll x, ll n){
    ll ret = 1;
    while(n){
        if(n&1) (ret*=x)%=mod;
        (x*=x)%=mod;
        n>>=1;
    }
    return ret;
}

ll mod_inv(ll x){
    return mod_pow(x, mod-2);
}

const int N = 6006;
ll f[N];
ll C(ll n, ll r){
    if(n<r) return 0;
    ll ret = f[n];
    (ret*=mod_inv(f[r]))%=mod;
    (ret*=mod_inv(f[n-r]))%=mod;
    return ret;
}

ll solve(){
    int n,r,g,b;
    cin >>n >>r >>g >>b;
    int S = r+g+b;

    if(S==0) return 1;
    if(S>n) return 0;

    ll ans = 0;
    // 2個の塊の個数
    rep(i,S){
        // 1個の塊の個数
        int O = S - 2*i;
        if(O<0) continue;
        // 最低でも必要な空白
        int sp = i+O-1;
        if(2*i + O + sp > n) continue;

        // 自由に挿入できる空白の個数
        int rsp = n - (2*i+O+sp);

        // 1個の塊と2個の塊の枠の配置方法
        ll t = C(i+O,i);
        // 残った空白の挿入方法
        (t *= C(i+O+rsp,rsp)) %= mod;
        // 2個の塊は、各セルごとに入れ替えられる
        (t *= mod_pow(2,i)) %= mod;

        rep(j,i+1){
            int rem_r = r-j;
            if(rem_r<0) continue;
            if(rem_r>O) continue;

            ll tt = t;
            // 2個の塊に赤を割り当てる
            (tt *= C(i,j)) %= mod;
            // 1個の塊に赤を割り当てる
            (tt *= C(O,rem_r)) %= mod;

            int rem_g = g-(i-j), rem_b = b-(i-j);
            if(rem_g<0 || rem_b<0) continue;
            // 残りのマスに2色を割り当てる
            (tt *= C(rem_g+rem_b,rem_g)) %= mod;

            (ans += tt) %= mod;
        }
    }
    return ans;
}

int main(){
    f[0] = 1;
    for(int i=1; i<N; ++i) f[i] = (f[i-1]*i)%mod;

    cout << solve() << endl;
    return 0;
}
0