-
Very simple. The answer is \lfloor{\frac{mn}{2}}\rfloor for all cases. If mn is even then you can get \frac{mn}{2} as the entire box can be filled with the sweets in this case. When mn is odd, you can tile the entire board except one square.This gives \frac{mn-1}{2} sweets. In either case the answer is \lfloor{\frac{mn}{2}}\rfloor.
Complexity per test case O(1).
Code is here. -
Nice DP. Truth be told Idk how my solution passed. The constraints were 10^7 and my sol was n^2!
The idea is to make a dp array where dp[i] stores the least number of hops required to reach the i th position. Note that we can jump from a certain index j to another larger index i if and only if j + a[j] \ge i. and this takes 1 + dp[j] hops. So we can initially set a[i] = INF and then iterate through the array looking for indexes that satisfy j + a[j] \ge i and try to minimize dp[i]. Then our final answer is dp[i] if it is less than INF and -1 otherwise.
Complexity per test case O(n^2)
In the implementation INF = 1e9.
Code is here
Wrong implementations are here and here.Idk why you would want to view them but there they are. -
This one was a implementation problem. The idea is to precompute an array a[i] that stores the number of digits 7 in the range [0, i]. We can compute this by defining a function count7(n) that stores the number of 7 s in the base 10 representation of n. This function can be implemented to work in O(\log n) time. Now we can find a[i] by using a[i]= a[i-1] + count7(i). and a[0]=0.
After computing this we can compute the answer of each pair (x, y) as follows. Our answer is just a[y] - a[x-1]. Convinced? You shouldn’t be! What happens if x = 0? To fix this we can just replace x with 1 and it will still be fine.(because 0 has no 7 's) Look into the code for more info on this.
Complexity is O(1) per test case after the precomputation which is n\log n
Code is here
Hope this helps…