A. Subarrays Beauty
Consider each bit on its own since the AND operation is bit-wise.
For each bit, notice that any sub-array that has a zero bit will add nothing to the answer.
For each bit, A sub-array of only ones of length X has sub-array of only ones, and each of them will add 2W where W is the Weight of the bit to the answer.
justHusam solution: https://ideone.com/PL7s1U
Vendetta. solution: https://ideone.com/23iukT
B. Array Reconstructing
You can construct the array from only one known element.
justHusam solution: https://ideone.com/IDqwCe
C. Large Summation
All numbers are less than 109 + 7, so the summation of any 2 numbers is either less than 109 + 7 or less than 2 × (109 + 7).
Sort the elements and for each element use binary search to handle each of the cases above to find the best answer.
justHusam solution: https://ideone.com/1mSW7q
Vendetta. solution: https://ideone.com/V2wqJT
D. Counting Test
To make things easier, use the following concept: Let's make a function F(N) that finds the answer of range [0, N], then the answer of range [L, R] would be: F(R) - F(L - 1).
Make a frequency array of the original string then find how many times the string was repeated from 0 to N, the number of times is where |S| is the length of the original string.
If is not zero then we have extra characters that doesn't make a full string, we can make 26 frequency arrays for each letter in alphabet and do a prefix sum to get the number of times a specific letter was repeated in the first letters of the original string.
justHusam solution: https://ideone.com/BbGc7w
Complexity: O(26N + Q).
E. Game of Dice
Meet in the middle, divide the dice into 2 groups of equal sizes and brute force each part on it's own and store the in 2 vectors V1, V2.
For each element in V1, we must find how many elements in V2 satisfies the equality: .
Let's call the element from V1 as A and the element from V2 as B and we want to find the value of B:
multiply both sides by :
where is the inverse of A respective to the mod 109 + 7.
Since all values of V2 are below 109 + 7, then there's only one value for B in V2 that satisfies the equality (because all values B + K(109 + 7) satisfies the equality for any natural number K).
So we will calculate the value of B and check how many times was it repeated in V2.
justHusam solution: https://ideone.com/pFtGym
Vendetta. solution: https://ideone.com/udraup
Note: O(6N) gives TLE because 614 is approximately 78 × 109.
F. Strings and Queries
Find the number of palindrome sub-strings for each string, using an algorithm that runs worse than O(L2) gives TLE where L is the length of the string.
The problem is now reduces to RMQ (Range Max Query), we can now use data structures like Sparse Table, Segment Tree ... but Sparse Table is preferred as it runs faster than Segment Tree.
We still have a problem that the queries are given as strings not indices, to solve this we can map the strings to their indices, but using a map of strings will give TLE as the factor for comparing strings isn't small, to solve this was can use Hashing or Trie to get the index of the string fast.
Hashing will take O(L + log(N)) per query to hash and look for the hash value in a map of hash values.
Trie will take only O(L) per query to track the string in the Trie, but Trie needs pre-construction of O(LN).
Sparse Table with Hashing solution: https://ideone.com/iVZNHd (Running Time: 1621 ms)
Segment Tree with Hashing solution: https://ideone.com/UPsA6o (Running Time: 2042 ms)
Complexity: O(NL2 + NLog(N) + Q(L + Log(N))).
Sparse Table with Trie solution: https://ideone.com/pn4haB (Running Time: 1716 ms)
Complexity: O(NL2 + NLog(N) + NL + QL).
Note: You don't need to worry about collision in hashing since you don't need to use at all, the max hash value will be 430 which is approximately 1018 which fits into
G. Magical Indices
Construct 2 arrays, MX and MN where MXi is the maximum value in the range [1, i] and MNi is the minimum value in range [i, N].
For each element X, if the max value before it is less than X and the min value after it is more than X then add one to the answer.
justHusam solution: https://ideone.com/9SzqJb
H. Corrupted Images
Count the number of ones on the border, let's call it Y.
There are total of X = N + N + M + M - 4 border cells, the answer is X - Y.
There is no solution when the total number of ones in the whole image is less than X, then we can't cover the whole border with ones.
justHusam solution: https://ideone.com/wS5TPl
I. The Crazy Jumper
Construct a graph, add edge from cell i to cell i + 1 and from cell i to cell j where cell j has same color of cell i and there are no cells between i and j that also have the same color.
Using dynamic programming dpi = 1 + min(dpi + 1, dpnexti), try to jump to the next cell or to the first call that has the same color.
DP top-down: https://ideone.com/UnBst5
DP buttom-up: https://ideone.com/ysugUh
J. The Hell Boy
To be able to find this solution, you need to work on a paper, let's suppose we want to find the answer for 3 numbers, A B and C.
The terms of those 3 will be:
A + B + C + AB + AC + BC + ABC
Take C as a common factor from the terms that has C:
A + B + AB + C(1 + A + B + AB)
Notice that if X = A + B + AB then:
X + C(1 + X)
Now take B as a common factor as well:
A + B(1 + A) + C(1 + A + B(1 + A))
Suppose the answer for the problem is X, we can notice that if we add a new number Y, the answer will be X + Y(1 + X).
Using dynamic programming, dpi = dpi + 1 + Ai × dpi + 1, the DP tries to either take the element or not, we will have to subtract 1 from the answer as the DP will consider the empty sub-set as valid sub-set which will add one to the wanted answer.
K. Palindromes Building
If there are more than one letter with odd frequency then we can't make any palindromes.
Since the limits are too small, we can brute force and build all the palindromes, since the first half of the palindrome is the same as the second half but reversed, we can only generate all the palindromes by generating the first half only, make a string containing half of the frequency for each letter in the original string and use
next_permutation to check how many different permutations you can get.
There's also another faster solution if the constraints were bigger, to know how many different ways we form a string using half of the letters to make the first half of the palindrome, the answer will be: , to eliminate the repeated permutations we divide by the factorial of half of the frequencies for each letter, basic counting math.
next_permutation solution: https://ideone.com/zaXvKc
Note: O(N!) gives TLE because 20! is approximately 2.4 × 1018.
Vendetta. math solution: https://ideone.com/0BNI2r