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

qqq번 질의의 값을 N,M,a,bN,M,a,bN,M,a,b라 하자.

MMM번 누적합한 뒤의 마지막 항에서 원래 항 Bi(0)B^{(0)}_iBi(0)​가 더해지는 횟수는

(N−i+M−1M−1)\binom{N-i+M-1}{M-1}(M−1N−i+M−1​)

이다. 따라서 답은 다음과 같다.

∑i=1N(ai+b)(N−i+M−1M−1)\sum_{i=1}^{N} (a i+b)\binom{N-i+M-1}{M-1}i=1∑N​(ai+b)(M

조합 항등식

∑i=1N(N−i+M−1M−1)=(N+M−1M)\sum_{i=1}^{N}\binom{N-i+M-1}{M-1}=\binom{N+M-1}{M}i=1∑N​(M−1

와

∑i=1Ni(N−i+M−1M−1)=(N+MM+1)\sum_{i=1}^{N} i\binom{N-i+M-1}{M-1}=\binom{N+M}{M+1}i=1∑N​i(M−

를 사용하면 답은

a(N+MM+1)+b(N+M−1M)a\binom{N+M}{M+1}+b\binom{N+M-1}{M}a(M+1N+M​)+b(

이다.

모든 질의에서 필요한 조합의 위쪽 값은 최대 N+M≤400 000N+M \leq 400\ 000N+M≤400 000이다. P=1 000 000 007P=1\ 000\ 000\ 007P=1 000 000 007은 소수이므로 팩토리얼과 역팩토리얼을 400 000400\ 000400 000까지 전처리하면 각 질의를 에 처리할 수 있다.

전체 시간 복잡도는 O(Q+400 000)O(Q+400\ 000)O(Q+400 000)이다.

Solution written by GPT5.5

−
1
N−i+M−1
​
)
N−i+M−1
​
)
=
(MN+M−1​)
1
N−i+M−1
​
)
=
(M+1N+M​)
M
N+M−1
​
)
O(1)O(1)
O(1)