665A - Buses Between Cities
The problem was suggested by Sergey Erlikh unprost.
Consider the time interval when Simion will be on the road strictly between cities (x1, y1) (x1 = 60h + m, y1 = x1 + ta). Let's iterate over the oncoming buses. Let (x2, y2) be the time interval when the oncoming bus will be strictly between two cities. If the intersection of that intervals (x = max(x1, x2), y = min(y1, y2)) is not empty than Simion will count that bus.
Complexity: O(1).
665B - Shopping
The problem was suggested by Ayush Anand JeanValjean01.
In this problem you should simply do what was written in the problem statement. There are no tricks.
Complexity: O(nmk).
665C - Simple Strings
The problem was suggested by Zi Song Yeoh zscoder.
There are two ways to solve this problem: greedy approach and dynamic programming.
The first apprroach: Considerr some segment of consecutive equal characters. Let k be the length of that segment. Easy to see that we should change at least characters in the segment to remove all the pairs of equal consecutive letters. On the other hand we can simply change the second, the fourth etc. symbols to letter that is not equal to the letters before and after the segment.
The second approach: Let zka be the minimal number of changes so that the prefix of length k has no equal consecutive letters and the symbol s'k equals to a. Let's iterate over the letter on the position k + 1 and if it is not equal to a make transition. The cost of the transition is equal to 0 if we put the same letter as in the original string s on the position k + 1. Otherwise the cost is equal to 1.
Complexity: O(n).
665F - Four Divisors
The editorial for this problem is a little modification of the materials from the lecture of Mikhail Tikhomirov Endagorion of the autumn of 2015 in Moscow Institute of Physics and Technology. Thanks a lot to Endagorion for that materials.
Easy to see that only the numbers of the form p·q and p3 (for different prime p, q) have exactly four positive divisors.
We can easily count the numbers of the form p3 in , where n is the number from the problem statement.
Now let p < q and π(k) be the number of primes from 1 to k. Let's iterate over all the values p. Easy to see that . So for fixed p we should increase the answer by the value .
So the task is ot to find — the number of primes not exceeding , for all p.
Denote pj the j-th prime number. Denote dpn, j the number of k such that 1 ≤ k ≤ n, and all prime divisors of k are at least pj (note that 1 is counted in all dpn, j, since the set of its prime divisors is empty). dpn, j satisfy a simple recurrence:
dpn, 1 = n (since p1 = 2)
dpn, j = dpn, j + 1 + dp⌊ n / pj⌋, j, hence dpn, j + 1 = dpn, j - dp⌊ n / pj⌋, j
Let pk be the smallest prime greater than . Then π(n) = dpn, k + k - 1 (by definition, the first summand accounts for all the primes not less than k).
If we evaluate the recurrence dpn, k straightforwardly, all the reachable states will be of the form dp⌊ n / i⌋, j. We can also note that if pj and pk are both greater than , then dpn, j + j = dpn, k + k. Thus, for each ⌊ n / i⌋ it makes sense to keep only values of dp⌊ n / i⌋, j.
Instead of evaluating all DP states straightforwardly, we perform a two-step process:
Choose K.
Run recursive evaluation of dpn, k. If we want to compute a state with n < K, memorize the query ``count the numbers not exceeding n with all prime divisors at least k''.
Answer all the queries off-line: compute the sieve for numbers up to K, then sort all numbers by the smallest prime divisor. Now all queries can be answered using RSQ structure. Store all the answers globally.
Run recurisive evaluation of dpn, k yet again. If we want to compute a state with n < K, then we must have preprocessed a query for this state, so take it from the global set of answers.
The performance of this approach relies heavily on Q — the number of queries we have to preprocess.
Statement. .
Proof:
Each state we have to preprocess is obtained by following a dp⌊ n / pj⌋, j transition from some greater state. It follows that Q doesn't exceed the total number of states for n > K.
The preprocessing of Q queries can be done in , and it is the heaviest part of the computation. Choosing optimal , we obtain the complexity .
Complexity: .