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

Let's Play with Functions

Back to problem

Editorial

For every positive integer nnn, we first prove f(n)=2nf(n)=2^nf(n)=2n. The base case f(1)=2f(1)=2f(1)=2 holds. Moreover,

f(n+1)=f(n)f(1)=2f(n),f(n+1)=f(n)f(1)=2f(n),f(n+1)=f(n)f(1)=2f(n),

so the claim follows by induction.

For each query, we must compute 2ni mod M2^{n_i}\bmod M2ni​modM, where M=109+7M=10^9+7M=. Since is prime and coprime to , Fermat's little theorem gives

2M−1≡1(modM).2^{M-1}\equiv1\pmod M.2M−1≡1(modM).

Thus, if eie_iei​ is the remainder of nin_ini​ modulo M−1=109+6M-1=10^9+6M−, then

2ni≡2ei(modM).2^{n_i}\equiv2^{e_i}\pmod M.2ni​≡2e

Because nin_ini​ can be as large as 1020010^{200}10200, read it as a decimal string. Process its digits d1,d2,⋯ ,dkd_1,d_2,\cdots,d_kd from left to right. Starting with , update

e←(10e+dj) mod (M−1).e\leftarrow(10e+d_j)\bmod(M-1).e←(10e+dj​)mod(M−

Finally, compute 2e mod M2^e\bmod M2emodM with binary exponentiation.

If query iii has LiL_iLi​ decimal digits, the total time complexity is O(∑i=1QLi+Qlog⁡M)O(\sum_{i=1}^Q L_i+Q\log M)O(∑i. The extra space, excluding the input string, is . The maximum input string length is .

Solution written by GPT6

1
09
+
7
MMM
222
1
=
109+
6
i
​
(modM).
1
​
,
d2​
,
⋯
,
dk​
e=0e=0e=0
1
)
.
=
1
Q
​
Li​
+
QlogM)
O(1)O(1)O(1)
201201201