結果

問題 No.584 赤、緑、青の色塗り
ユーザー gew1fw
提出日時 2025-06-12 20:50:01
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,789 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 81,672 KB
実行使用メモリ 274,380 KB
最終ジャッジ日時 2025-06-12 20:53:34
合計ジャッジ時間 5,342 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 7 TLE * 1 -- * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def main():
    import sys
    input = sys.stdin.read
    N, R, G, B = map(int, input().split())
    total = R + G + B
    if total > N:
        print(0)
        return

    from collections import defaultdict

    # Initialize DP
    dp = defaultdict(int)
    dp[(0, 0, None, R, G, B)] = 1  # (i, run_length, last_color, r, g, b)

    colors = ['R', 'G', 'B']
    color_map = {'R': (R, 0, 0), 'G': (0, G, 0), 'B': (0, 0, B)}

    for i in range(N):
        current_dp = dp
        next_dp = defaultdict(int)
        for (pos, run_length, last_color, r, g, b) in current_dp:
            if pos != i:
                continue  # Only process the current position
            count = current_dp[(pos, run_length, last_color, r, g, b)]

            # Option 1: Leave the next cell blank
            next_run_length = 0
            next_last_color = None
            next_r, next_g, next_b = r, g, b
            next_pos = i + 1
            next_dp[(next_pos, next_run_length, next_last_color, next_r, next_g, next_b)] += count
            next_dp[(next_pos, next_run_length, next_last_color, next_r, next_g, next_b)] %= MOD

            # Option 2: Color the next cell
            for color in colors:
                # Check if we have remaining count for this color
                if color == 'R' and r == 0:
                    continue
                if color == 'G' and g == 0:
                    continue
                if color == 'B' and b == 0:
                    continue

                # Check if previous color is same
                if run_length >= 1 and color == last_color:
                    continue

                # Check if run_length is already 2
                if run_length == 2:
                    continue

                new_run_length = run_length + 1
                new_last_color = color
                if color == 'R':
                    new_r = r - 1
                    new_g = g
                    new_b = b
                elif color == 'G':
                    new_r = r
                    new_g = g - 1
                    new_b = b
                else:
                    new_r = r
                    new_g = g
                    new_b = b - 1

                next_pos = i + 1
                next_dp[(next_pos, new_run_length, new_last_color, new_r, new_g, new_b)] += count
                next_dp[(next_pos, new_run_length, new_last_color, new_r, new_g, new_b)] %= MOD

        dp = next_dp

    # Sum all valid end states where i == N
    result = 0
    for (pos, run_length, last_color, r, g, b) in dp:
        if pos == N and r == 0 and g == 0 and b == 0:
            result += dp[(pos, run_length, last_color, r, g, b)]
            result %= MOD
    print(result % MOD)

if __name__ == '__main__':
    main()
0