DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

WhiteBlue

Back to problem

Editorial

Represent each column by an NNN-bit mask, using bit 000 for white and bit 111 for blue.

First retain only masks with no three equal consecutive bits. Let VVV be this set and K=∣V∣K=|V|K=∣V∣. Since N≤8N\le 8N≤8, we have K≤68K\le 68K≤68. For each column, a selected mask must also agree with all prepainted cells.

Only the last two columns are needed to check the horizontal condition. Let dp[a][b]dp[a][b]dp[a][b] count valid paintings of the processed prefix whose last two column masks are a,ba,ba,b. We may append mask ccc exactly when, in every row, the corresponding three bits in a,b,ca,b,ca,b, are not all equal. Then add to , taking all values modulo .

If M=1M=1M=1, simply count the allowed masks for the first column. Otherwise initialize the DP using all allowed pairs of masks for the first two columns, process subsequent columns, and sum all final states.

Building the vertical-valid masks takes O(N2N)O(N2^N)O(N2N) time, checking compatibility with fixed cells takes O(MNK)O(MNK)O(MNK) time, and the DP takes O(MK3)O(MK^3)O(MK time. Precomputing the transitions uses memory. Each addition combines two reduced residues, so it fits in a signed 32-bit integer.

c
dp[a][b]dp[a][b]dp[a][b]
next[b][c]next[b][c]next[b][c]
1 000 000 0071\,000\,000\,0071000000007
3
)
O(K3+MK+K2)O(K^3+MK+K^2)O(K3+MK+K2)
Solution written by GPT6