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 account linking.

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

Changing Seats

Back to problem

Editorial

If person iii moves to a different seat jjj, the color rule gives j−i≡1(mod3)j-i\equiv1\pmod3j−i≡1(mod3). Together with the distance limit, this means the only possible seats are i−2i-2i−2, iii, and i+1i+1i+1, excluding any that do not exist.

Every valid arrangement can be divided from left to right into blocks of length 111, where one person stays, and blocks of length 333, where three consecutive people rotate. A block of length 333 starting at person iii has the following movements.

i→i+1,i+1→i+2,i+2→i.i\to i+1,\qquad i+1\to i+2,\qquad i+2\to i.i→i+1,i+1→i+2,i+2→

The people occupying these three seats are therefore (i+2,i,i+1)(i+2,i,i+1)(i+2,i,i+1) from left to right. Rotation in the opposite direction is not allowed.

Let dp[i]dp[i]dp[i] be the number of ways to fill the first iii seats using the first iii people. Set dp[0]=1dp[0]=1dp[0]=1 and initialize the remaining values to 00. For each from through , add the following two possibilities.

  • If Si≠MS_i\neq\text{M}Si​=M, the last person may stay, so add dp[i−1]dp[i-1]dp[i−1].
  • If and none of is , the last three people may rotate, so add .

Perform every addition modulo 1 000 000 0071\,000\,000\,0071000000007. The answer is dp[N]dp[N]dp[N]. Both the time complexity and the space complexity are O(N)O(N)O(N).

Proof

Suppose an initial segment of people has been assigned exactly to the seats in the same initial segment. Let iii be the smallest remaining person and seat number.

If person iii stays in seat iii, they form a block of length 111. Remove this person and seat and apply the same reasoning to the remainder.

If person iii moves, seat i−2i-2 has already been assigned or does not exist, so they must move to seat . The only people who could occupy the now-empty seat are , , and . Person has already been assigned or does not exist, while person has moved elsewhere. Thus, person must occupy seat .

Solution written by GPT6

i.
0
iii
111
NNN
i≥3i\geq3
i≥3
Si−2,Si−1,SiS_{i-2},S_{i-1},S_iSi−2​,Si−1​,Si​
F
dp[i−3]dp[i-3]dp[i−3]
i−2
i+1i+1i+1
iii
i−1i-1i−1
iii
i+2i+2i+2
i−1i-1i−1
iii
i+2i+2i+2
iii

Of the seats available to person i+1i+1i+1, seat i−1i-1i−1 has already been assigned or does not exist, and seat i+1i+1i+1 is occupied by person iii. Therefore, person i+1i+1i+1 must move to seat i+2i+2i+2. If any required person or seat does not exist, this case is impossible. Otherwise, these three consecutive people and seats form a complete block.

Repeating this argument gives a unique decomposition of every valid arrangement into blocks of length 111 or 333. Conversely, arranging each block as described uses every person and seat exactly once and satisfies the color and distance rules. A block of length 111 is allowed exactly when that person's condition is not M. A block of length 333 is allowed exactly when none of its three conditions is F.

The final block has length 111 or 333, and these cases are disjoint. For each valid arrangement of the preceding segment, there is exactly one way to append the chosen final block. Hence, the recurrence counts every valid arrangement exactly once.