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

Tile Covering

Back to problem

Editorial

Suppose tiles of length BBB are used xxx times. Since the two lengths alternate and both the first and last tiles have length AAA, tiles of length AAA are used x+1x+1x+1 times. Both tile lengths must be used, so x≥1x\geq1x≥1.

Therefore,

A(x+1)+Bx=(A+B)x+A=N.A(x+1)+Bx=(A+B)x+A=N.A(x+1)+Bx=(A+B)x+A=N.

Temporarily ignore the condition A≠BA\neq BA=B. Let S=A+BS=A+BS=A+B. Then

N=Sx+A.N=Sx+A.N=Sx+A.

Once SSS with 1≤S≤N1\leq S\leq N1≤S≤N is fixed, division with remainder uniquely determines xxx and AAA, and therefore also B=S−AB=S-A. Thus, we initially have choices for .

Now remove the invalid cases.

First, consider A=0A=0A=0. This happens exactly when the remainder of dividing NNN by SSS is zero, so it is equivalent to SSS being a divisor of NNN.

Second, consider A=BA=BA=B. In this case,

N=(2x+1)A.N=(2x+1)A.N=(2x+1)A.

Since S=2AS=2AS=2A,

2N=(2x+1)S.2N=(2x+1)S.2N=(2x+1)S.

If SSS divides NNN, then 2NS\frac{2N}{S}S2N​ is even. In the case A=BA=B, is odd. Therefore, these two kinds of invalid cases are disjoint.

Also, every positive divisor SSS of 2N2N2N except 2N2N2N itself satisfies S≤NS\leq NS≤N. If 2NS\frac{2N}{S} is even, then divides , corresponding to . If it is odd, it corresponds to . Hence the number of invalid choices is the number of positive divisors of , excluding itself.

Let D(M)D(M)D(M) denote the number of positive divisors of MMM. The answer is

N−D(2N)+1.N-D(2N)+1.N−D(2N)+1.

We can compute D(2N)D(2N)D(2N) by testing divisors up to 2N\sqrt{2N}2N​, giving an O(N)O(\sqrt N) algorithm.

Since the answer is N−D(2N)+1≤N≤1010N-D(2N)+1 \leq N \leq 10^{10}N−D(2N)+1≤N≤1010, it always fits in a signed 64-bit integer.

Solution written by GPT5.6

B
=
S−
A
NNN
SSS
A
=
B
2NS=2x+1\frac{2N}{S}=2x+1S2N​=2x+1
S2N​
SSS
NNN
A=0A=0A=0
A=BA=BA=B
2N2N2N
2N2N2N
O(N​)