Editorial for DMOPC '19 Contest 6 P3 - Grade 11 Math
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the first subtask, it suffices to process each query by simply iterating from
Time complexity:
For the full marks, observe that each bit in the string is set at most once. One way to take advantage of this is to use a set data structure such as std::set
in C++ or TreeSet
in Java to maintain the set of indices which have not been set yet. In doing this, each bit is removed from the set at most once, until the set is empty and all bits have been set. Then, for each query, search for the first element which is greater than
Pseudocode (set solution)
MODULO=1e9+7
function add(a, b)
return (a + b) mod MODULO
read |S|, M, S
unset = [0, |S|)
answer = 0
(pre-process powers of 2)
powers = [1]
for i in (0, M)
powers[i] = add(powers[i-1], powers[i-1])
reverse powers
do M times
read l, r
x is smallest number in unset >= l
while x <= r
ans = add(ans, powers[x])
remove x from unset
x is smallest number in unset >= l
print ans
Time complexity:
Comments
:tzak: