結果

問題 No.584 赤、緑、青の色塗り
ユーザー PachicobuePachicobue
提出日時 2018-09-15 03:49:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,190 ms / 2,000 ms
コード長 2,017 bytes
コンパイル時間 2,114 ms
コンパイル使用メモリ 204,060 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 20:43:42
合計ジャッジ時間 4,498 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
constexpr ll MOD = 1000000007LL;
template <ll mod = MOD>
class ModCombination
{
public:
    ModCombination(const std::size_t n) : fact(n + 1, 1), inv(n + 1, 1), inv_fact(n + 1, 1)
    {
        for (ll i = 2; i <= (ll)n; i++) { fact[i] = (fact[i - 1] * i) % mod, inv[i] = ((mod - (mod / i)) * inv[mod % i]) % mod, inv_fact[i] = (inv_fact[i - 1] * inv[i]) % mod; }
    }
    ll combination(const std::size_t n, const std::size_t k) const { return (((fact[n] * inv_fact[k]) % mod) * inv_fact[n - k]) % mod; }

private:
    std::vector<ll> fact, inv, inv_fact;
};

int main()
{
    int N, R, B, G;
    std::cin >> N >> R >> B >> G;
    const int MAX = (N + 1) / 3;
    ModCombination<> mod(N + 2);
    ll ans = 0;
    std::vector<ll> p2(N + 2, 1);
    for (int i = 1; i <= N + 1; i++) { p2[i] = p2[i - 1] * 2 % MOD; }
    for (int a = 0; a <= std::min({R, B, MAX}); a++) {
        for (int b = 0; b <= std::min({R - a, G, MAX - a}); b++) {
            for (int c = 0; c <= std::min({B - a, G - b, MAX - a - b}); c++) {
                const int d = R - a - b, e = B - a - c, f = G - b - c;
                const int sp = (N + 1) - 3 * a - 3 * b - 3 * c - 2 * d - 2 * e - 2 * f;
                if (sp < 0) { continue; }
                int sum = a + b + c + d + e + f + sp;
                ll p = p2[a + b + c];
                (p *= mod.combination(sum, a)) %= MOD;
                sum -= a;
                (p *= mod.combination(sum, b)) %= MOD;
                sum -= b;
                (p *= mod.combination(sum, c)) %= MOD;
                sum -= c;
                (p *= mod.combination(sum, d)) %= MOD;
                sum -= d;
                (p *= mod.combination(sum, e)) %= MOD;
                sum -= e;
                (p *= mod.combination(sum, f)) %= MOD;
                sum -= f;
                (ans += p) %= MOD;
            }
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0