DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

교대

Back to problem

Editorial

AAA와 BBB의 각 자릿수를 100100100진수의 한 자리처럼 해석하자. 어떤 양의 정수 XXX의 자릿수가 x1,x2,…,xkx_1,x_2,\ldots,x_kx1​,x2​,…,xk​라면

v(X)=x1⋅100k−1+x2⋅100k−2+⋯+xkv(X)=x_1\cdot 100^{k-1}+x_2\cdot 100^{k-2}+\cdots+x_kv(X)=x1​⋅100k−1

로 정의한다. 앞에 000을 붙여도 이 값은 변하지 않는다.

그러면 항상

A⋆B=10v(A)+v(B)A \star B = 10v(A)+v(B)A⋆B=10v(A)+v(B)

이다. 따라서 필요한 조건은

10v(A)+v(B)≡0(modP)10v(A)+v(B)\equiv 0 \pmod P10v(A)+v(B)≡0(modP)

이다.

먼저 v(B) mod Pv(B) \bmod Pv(B)modP를 왼쪽부터 한 자리씩 보며 계산한다. P=2P=2P=2 또는 P=5P=5P=5라면 는 항상 의 배수이므로, 일 때에만 답이 존재한다. 이 경우 최소 양의 정수는 이고, 그렇지 않으면 답은 이다.

이제 PPP가 2,52,52,5가 아닌 경우를 보자. PPP는 소수이므로 101010의 역원이 존재한다. 따라서 v(A)v(A)v(A)가 가져야 하는 나머지는

target≡−v(B)⋅10−1(modP)\mathrm{target}\equiv -v(B)\cdot 10^{-1}\pmod Ptarget≡−v(B)⋅10−1(modP

로 하나로 정해진다.

이제 v(A)≡target(modP)v(A)\equiv \mathrm{target}\pmod Pv(A)≡target(modP)를 만족하는 최소 양의 정수 AAA를 찾으면 된다. 현재 나머지가 rrr이고 새 십진수 자릿수 를 뒤에 붙이면 새로운 나머지는

(100r+x) mod P(100r+x)\bmod P(100r+x)modP

이다.

나머지 0,1,…,P−10,1,\ldots,P-10,1,…,P−1을 정점으로 두고 BFS를 수행한다. 시작 정점은 한 자리 수 1,2,…,91,2,\ldots,91,2,…,9가 만드는 나머지이며, 각 정점에서는 자릿수 0,1,…,90,1,\ldots,9를 붙이는 전이를 사용한다. 시작 자릿수와 전이 자릿수를 오름차순으로 처리하면 BFS에서 처음으로 목표 나머지에 도달했을 때의 문자열이 곧 최소 양의 정수이다.

정점 수는 PPP개이고 각 정점에서 최대 101010개의 전이를 확인하므로 시간 복잡도는 O(P+∣B∣)O(P+|B|)O(P+∣B∣), 메모리 복잡도는 O(P)O(P)O(P)이다.

Solution written by GPT5.5

+
x2​⋅
100k−2+
⋯+
xk​
10v(A)10v(A)
10v(A)
PPP
v(B)≡0(modP)v(B)\equiv 0 \pmod Pv(B)≡0(modP)
111
−1-1−1
)
xx
x
0
,
1
,
…
,
9