PROBLEM LINK:Setter Varad Kulkarni DIFFICULTY:SIMPLE PREREQUISITES:PROBLEM:Given a value of $N$, number of digits in a number $D_1D_2D_3...D_N$, and $W$ which is given by $W=\sum_{i=2}^N (D_i  D_{i1})\,$, we have to find number of $N$ digit numbers which have weight $W$ modulo ${10}^{9}+7$. QUICK EXPLANATION:We can reduce the expression to $W=D_nD_1$ where $D_n$ is the rightmost digit, and $D_1$ is the leftmost digit. We see that we can find valid numbers only for $9\le W \le 8$, for all other values of $W$ the answer is $0$. If $9\le W \le 8$, we brute force the pair of digits $(D_1,D_N)$ such that $D_ND_1=W$. Let this be denoted by $cnt$. Clearly, answer is $cnt*{10}^{N2}\% ({10}^{9}+7)$ EXPLANATION:" Oh...This question...This question has that $"\sum"$ symbol...It must be..It must be very difficult." If you left the question because of above belief, then dont read this editorial. You're gonna regret that decision of yours :( For this question, since Editorialist's , Tester's, and Setter's, all three have use a similar approach, so the approach will be described in a bit more detail. After discussing the intuition and approach, we will see tester's idea to further reduce time complexity in the bonus section :). Editorialist's/Tester's/Setter's Solution The very first thing we need to look at is the constraints. We have number of test cases, $T$ as $1\le T \le {10}^{5}$ and $2 \le N \le {10}^{18}$ , which clearly hints that we need to answer to answer each test case in $O(logN)$ or $O(1)$ complexity. With that in mind, we should first try to simplify the summation. Carefully look at the summation $W=\sum_{i=2}^N (D_i  D_{i1})\,$ and try to expand it. On expanding it, we get $W=\sum_{i=2}^N (D_i  D_{i1})\,$ $=(D_2D_1)+(D_3D_2)+(D_4D_3)......(D_ND_{N1})$ We see that we can cancel some terms above. Upon cancelling, we are left with $W=D_ND_1$ where $0 \le D_i\le9$. This means $W$ is nothing but difference of first and last digit of the number. This restricts valid values of $W$ to $9\le W \le 8$. No answer exists for any value outside this range. The first conclusion, hence, is that answer for $W$ outside the range $[9,8]$ is $0$ as no such $N$ digit number exists. Now, how to find $W$ for $9\le W \le 8$ $?$ We notice that for answer to exist, we need to put adjust only $D_n$ and $D_1$, while any other digit $D_i$ in between can take any value from $[0,9]$. Lets focus on valid pairs of $(D_1,D_N)$ right now. What we can do is, we can bruteforce all possible pairs of $(D_1,D_N)$, and count the number of pairs satisfying $D_ND_1=W$. Let this be denoted by $cnt$. Or, you can simply find the number of valid pairs by this formula
Please note that $D_1$ (leftmost digit) cannot be $0$ as leading $0's$ are not allowed. Now we have the number of valid pairs of $(D_1,D_N)$ in $cnt$. What about the rest of the digits? We see that the answer is independent of all other digits. Thus, all the remaining $N2$ digits can take any value between $[0,9]$. So, we have $cnt$ choices for $D_1,D_N$ and ${10}^{N2}$ choices for rest of the $N2$ digits. Thus, the answer will be $cnt*{10}^{N2}\% ({10}^{9}+7)$. We will calculate ${10}^{N2}\% ({10}^{9}+7)$ using fast exponentiation. (Link to algo given under prerequisites). $Time$ $Complexity=$ $O(T*LogN)$ SOLUTION:Setter CHEF VIJJU'S CORNER:1.During contest, this question was flooded with comments with everyone speaking the same thing  "Are constraints wrong?" or "How can W be >10? Why do constraints have W upto 300?" . &etc. While almost EVERYONE derived that formula that $W=D_ND_1$, they all got stuck at this trivial stuff that why is $W$ upto 300 in problem statement. Whats worse, they started feeling that since $W\le 300$ in the question, perhaps their derivation and understanding of the question is wrong. I have nothing to say here, except that if you yourself arent confident at your solution, dont expect anyone else to. Have confidence in what you do, and at the same time learn to accept when you go wrong. Balance these two things, and thats the secret of life :). 2. I have to mention Misha's immense variety of solution. If you see tester's code (the $O(Log(MOD))$ one), he has used something different. He used 3. Dont open the tab below. Now, Misha did not want to stop at $O(Log(MOD)$ solution, so he devised an $O(1)$ solution! Basically, that solution uses preprocessing. What he did is, we know that $MOD <{2}^{30}$. Hence, the highest value which we deal with can be represented within $30$ bits. He took the binary representation of number , and partitioned it into 3 parts, consisting 10 bits each. (Remember we can represent the numbers within 30 bits). Now, he uses his $POW00[],POW10[],POW20[]$ arrays to calculate any power of $10$ from $[0,MOD]$ in $O(1)$ time. $POW00[]$ is simple, it just stores powers of 10 mod ${10}^{9}+7$ for powers from $[0,1023]$. Imagine it like this, we partitioned the $30bit$ number into $3$ part of $10$ $bits$ each. $POW00[]$ deals with lowest 10 bits. Multiplying previous element by $10$ is like adding $1$ to power of previous element, (which , in terms of binary numbers, is essentially adding $1$ to binary representation of the number. ) Recurrence formula $POW00[i]=10*POW[i1];$ He does the same in $POW10[]$ array. He adds $1$ to binary representation of number but he is careful of one thing. $POW00[]$ dealt with numbers from $[0,{2}^{10}1]$, where the value of $LSB$ is 1. However, in case of $POW10[]$, the $LSB$ is (in reality) the $11$ $'th$ bit of the number. Hence, we multiply it with ${10}^{1024}$ instead of $10$. $POW10[i]={10}^{{2}^{10}}*POW10[i1];$ Similarly, in $POW20[]$, the recurrence relation is $POW20[i]={10}^{{2}^{20}}*POW20[i1]$. Its basically, he is adding $1$ to the $LSB$ of the $10bit$ partition. And adding one there, means we must multiply it with 10, raised to power equal to value of that $LSB$. His generator code is given below for the curious :). 4. Some of the good questions on fast exponentiation
This question is marked "community wiki".
asked 08 Apr '18, 03:51

Well Explained but solution are not opening :( answered 17 Apr '18, 16:56
Solutions will take some time as @admin links them to the page where they are uploaded. Sorry :( But, I made sure to comment almost all of my solutions so that theres a commented code :)
(17 Apr '18, 17:02)

My approach
answered 17 Apr '18, 17:00

Include this problem in practice section with reduced TL if possible:) answered 17 Apr '18, 18:04
Why reduced TL?
(17 Apr '18, 18:07)
Then i think everybody will try out the O(1) approach.
(17 Apr '18, 18:45)
Nooo. xD. Thats a good approach, but lets not force it. Thing is, at times, making LogN solution fail, fails some of O(1) solutions as well due to tight time limit. Also, question would be really complex and tough if we force that xD. Willing can try, else its fine. I will tell Misha you liked her approach though :p
(17 Apr '18, 19:12)
Yes as u say. actually did this problem in just around 5 minutes in practice section during contest with straight forward fast expo trick. Then today after this O(1) approach i was like ........ and btw her,i thought him:)
(17 Apr '18, 20:07)
xD . I am glad people from all divisions are liking the bonus contents. Really have to thank Misha for that though. Misha is great :D :)
(17 Apr '18, 20:12)
Yes,Misha is great.
(17 Apr '18, 20:26)
4
Yes, Misha is great :D If it would be "she" xD
(18 Apr '18, 01:11)
showing 5 of 7
show all

@jagreetdg u missed AC due to a typo:( mod is 1e9+7 not 10e9+7. 1e9+7 > 1000000007(10^9+7) answered 18 Apr '18, 22:33
His solution is
(18 Apr '18, 22:49)
0.00 seconds and 0M ,i see. hahahahahaha
(18 Apr '18, 22:54)

Is fast exponentiation same as modular exponentiation? answered 20 Apr '18, 10:14
1
Yes, @pavitra_ag Here we are calculating modular exponentiation. Name of method used is fast exponentiation. As the name suggest it is faster method to calculate power of a no. Sometimes both are used interchangeably to mean same.
(20 Apr '18, 10:20)
1
Means calculate a to the power b and then take mod n. This method takes O(logn) time. And is quite fast to calculate a to the power b and then take mod n.
(20 Apr '18, 10:21)
2
Yes, you can call them essentially the same, aside from minor difference @aryanc403 pointed out. People use them interchangeably :)
(20 Apr '18, 14:25)
