結果

問題 No.584 赤、緑、青の色塗り
ユーザー ryoissyryoissy
提出日時 2017-10-27 23:27:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,146 ms / 2,000 ms
コード長 1,626 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 146,356 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-14 05:01:44
合計ジャッジ時間 3,501 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

ll fact[10001];
ll fact_inv[10001];
ll twop[3001];

ll extgcd(ll a,ll b,ll& x,ll& y){
	ll d=a;
	if(b!=0LL){
		d=extgcd(b,a%b,y,x);
		y-=(a/b)*x;
	}else{
		x=1;
		y=0;
	}
	return d;
}

ll mod_inverse(ll a,ll m){
	ll x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}

int n,r,g,b;

int main(void){
	fact[0]=1;
	for(ll i=1;i<=10000;i++){
		fact[i]=fact[i-1]*i%MOD;
	}
	fact_inv[0]=1;
	for(ll i=1;i<=10000;i++){
		fact_inv[i]=fact_inv[i-1]*mod_inverse(i,MOD)%MOD;
	}
	twop[0]=1;
	for(int i=1;i<=2000;i++){
		twop[i]=twop[i-1]*2LL%MOD;
	}
	scanf("%d%d%d%d",&n,&r,&g,&b);
	ll ans=0;
	for(int i=0;i<=min(r,g);i++){
		if(i*3-1>n)continue;
		for(int j=0;j<=min(b,r-i);j++){
			if((i+j)*3-1>n)continue;
			for(int k=0;k<=min(g-i,b-j);k++){
				int restr=r-i-j;
				int restb=b-j-k;
				int restg=g-i-k;
				if((i+j+k)*3+(restr+restb+restg)*2-1>n)continue;
				int rest=n-((i+j+k)*3+(restr+restb+restg)*2-1);
				ll ck=fact[i+j+k+restr+restb+restg];
				ck=ck*fact_inv[i+j+k]%MOD;
				ck=ck*fact_inv[restr+restb+restg]%MOD;

				ll cb=fact[i+j+k];
				cb=cb*fact_inv[i]%MOD;
				cb=cb*fact_inv[j]%MOD;
				cb=cb*fact_inv[k]%MOD;

				ll ci=fact[restr+restb+restg];
				ci=ci*fact_inv[restr]%MOD;
				ci=ci*fact_inv[restb]%MOD;
				ci=ci*fact_inv[restg]%MOD;
				ck=ck*cb%MOD;
				ck=ck*ci%MOD;
				ck=ck*twop[i+j+k]%MOD;
				
				ck=ck*fact[rest+i+j+k+restr+restg+restb]%MOD;
				ck=ck*fact_inv[rest]%MOD;
				ck=ck*fact_inv[i+j+k+restr+restg+restb]%MOD;
				ans=(ans+ck)%MOD;
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}
0