A. Subarrays Beauty
Hint 1Consider each bit on its own since the AND operation is bit-wise.
Hint 2For each bit, notice that any sub-array that has a zero bit will add nothing to the answer.
Hint 3For 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
Complexity: O(Nlog(Max(Ai))).
B. Array Reconstructing
Hint 1You can construct the array from only one known element.
justHusam solution: https://ideone.com/IDqwCe
Complexity: O(N).
C. Large Summation
Hint 1All 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).
Hint 2Sort 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
Complexity: O(NLog(N)).
D. Counting Test
Hint 1To 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).
Hint 2Make 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.
justHusam solution: https://ideone.com/BbGc7w
Complexity: O(26N + Q).
E. Game of Dice
Hint 1Meet 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.
Hint 2For each element in V1, we must find how many elements in V2 satisfies the equality: .
justHusam solution: https://ideone.com/pFtGym
Vendetta. solution: https://ideone.com/udraup
Complexity: .
Note: O(6N) gives TLE because 614 is approximately 78 × 109.
F. Strings and Queries
Hint 1Find 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.
Hint 2The 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.
Hint 3We 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 long long
.
G. Magical Indices
Hint 1Construct 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].
Hint 2For 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
Complexity: O(N).
H. Corrupted Images
Hint 1Count the number of ones on the border, let's call it Y.
Hint 2There are total of X = N + N + M + M - 4 border cells, the answer is X - Y.
Hint 3There 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
Complexity: O(NM).
I. The Crazy Jumper
Solution 1Construct 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.
Solution 2Using 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.
justHusam solutions:
BFS: https://ideone.com/P2yACH
DP top-down: https://ideone.com/UnBst5
DP buttom-up: https://ideone.com/ysugUh
Complexity: O(N).
J. The Hell Boy
Solution 1To 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).
Solution 2Using 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.
Vendetta. solutions:
Math: https://ideone.com/rYmHOD
DP: https://ideone.com/IDWlkQ
Complexity: O(N).
K. Palindromes Building
Hint 1If there are more than one letter with odd frequency then we can't make any palindromes.
Hint 2Since 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.
Hint 3There'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.
justHusam next_permutation
solution: https://ideone.com/zaXvKc
Complexity: .
Note: O(N!) gives TLE because 20! is approximately 2.4 × 1018.
Vendetta. math solution: https://ideone.com/0BNI2r
Complexity: O(N).
in Problem k i use next_permutation for half string and i get ac 33950913
Auto comment: topic has been updated by Vendetta. (previous revision, new revision, compare).