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

(Insert Name Here) Sequence

Back to problem

Editorial

First consider N≤3N\leq3N≤3.

  • For N=1N=1N=1, the definition gives A=0<S=1A=0<S=1A=0<S=1.
  • For N=2N=2N=2, the only adjacent OR is 1OR2=3=S1\mathbin{\mathrm{OR}}2=3=S1OR2=3=S.
  • For N=3N=3N=3, the bitwise OR of any two distinct values is 333, so A=6=SA=6=SA=6=S.

Thus, no valid permutation exists in these three cases.

Now suppose N≥4N\geq4N≥4. It is enough to print

P=(1,2,3,…,N).P=(1,2,3,\ldots,N).P=(1,2,3,…,N).

For every iii, we have iOR(i+1)≥i+1i\mathbin{\mathrm{OR}}(i+1)\geq i+1iOR(i+1)≥i+1. Hence,

A≥∑i=1N−1(i+1)=S−1.A\geq\sum_{i=1}^{N-1}(i+1)=S-1.A≥i=1∑N−1​(i+1)=S

The first term is 1OR2=31\mathbin{\mathrm{OR}}2=31OR2=3, which is one greater than the value 222 used in this lower bound. Since the value 444 exists, the term 3OR4=73\mathbin{\mathrm{OR}}4=73OR is three greater than the corresponding lower-bound value . Therefore,

A≥(S−1)+1+3>S.A\geq(S-1)+1+3>S.A≥(S−1)+1+3>S.

The time complexity is O(N)O(N)O(N), equal to the output size, and the additional space complexity is O(1)O(1)O(1).

Solution written by GPT6

−
1.
4=
7
444