### 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 (*x*_{1}, *y*_{1}) (*x*_{1} = 60*h* + *m*, *y*_{1} = *x*_{1} + *t*_{a}). Let's iterate over the oncoming buses. Let (*x*_{2}, *y*_{2}) be the time interval when the oncoming bus will be strictly between two cities. If the intersection of that intervals (*x* = *max*(*x*_{1}, *x*_{2}), *y* = *min*(*y*_{1}, *y*_{2})) is not empty than Simion will count that bus.

**С++ solution**

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.

**C++ solution**

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.

**Greedy approach on C++**

The second approach: Let *z*_{ka} 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.

**DP solution on C++**

Complexity: *O*(*n*).

### 665D - Simple Subset

The problem was suggested by Zi Song Yeoh zscoder.

Consider the subset *A* that is the answer to the problem. Let *a*, *b*, *c* be the arbitrary three elements from *A* and let no more than one of them is equal to 1. By the pigeonhole principle two of three elements from *a*, *b*, *c* have the same parity. So we have two integers with even sum and only one of them is equal to 1, so their sum is also greater than 2. So the subset *A* is not simple. In this way *A* consists of only two numbers greater than one (with a prime sum) or consists of some number of ones and also maybe other value *x*, so that *x* + 1 is a prime.

We can simply process the first case in *O*(*n*^{2}) time. The second case can be processed in linear time. Also we should choose the best answer from that two.

To check the value of order 2·10^{6} for primality in *O*(1) time we can use the simple or the linear Eratosthenes sieve.

**C++ solution**

Complexity: *O*(*n*^{2} + *X*), where *X* is the maximal value in *a*.

### 665E - Beautiful Subarrays

The problem was suggested by Zi Song Yeoh zscoder.

The sign is used for the binary operation for bitwise exclusive or.

Let *s*_{i} be the xor of the first *i* elements on the prefix of *a*. Then the interval (*i*, *j*] is beautiful if . Let's iterate over *j* from 1 to *n* and consider the values *s*_{j} as the binary strings. On each iteration we should increase the answer by the value *z*_{j} — the number of numbers *s*_{i} (*i* < *j*) so . To do that we can use the trie data structure. Let's store in the trie all the values *s*_{i} for *i* < *j*. Besides the structure of the trie we should also store in each vertex the number of leaves in the subtree of that vertex (it can be easily done during adding of each binary string). To calculate the value *z*_{j} let's go down by the trie from the root. Let's accumulate the value *cur* equals to the xor of the prefix of the value *s*_{j} with the already passed in the trie path. Let the current bit in *s*_{j} be equal to *b* and *i* be the depth of the current vertex in the trie. If the number *cur* + 2^{i} ≥ *k* then we can increase *z*_{j} by the number of leaves in vertex , because all the leaves in the subtree of tha vertex correspond to the values *s*_{i} that for sure gives . After that we should go down in the subtree *b*. Otherwise if *cur* + 2^{i} < *k* then we should simply go down to the subtree and recalculate the value *cur* = *cur* + 2^{i}.

**C++ solution**

Comlpexity by the time and the memory: *O*(*nlogX*), where *X* is the maximal xor on the prefixes.

### 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 *p*^{3} (for different prime *p*, *q*) have exactly four positive divisors.

We can easily count the numbers of the form *p*^{3} 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 *p*_{j} the *j*-th prime number. Denote *dp*_{n, j} the number of *k* such that 1 ≤ *k* ≤ *n*, and all prime divisors of *k* are at least *p*_{j} (note that 1 is counted in all *dp*_{n, j}, since the set of its prime divisors is empty). *dp*_{n, j} satisfy a simple recurrence:

*dp*_{n, 1}=*n*(since*p*_{1}= 2)*dp*_{n, j}=*dp*_{n, j + 1}+*dp*_{⌊ n / pj⌋, j}, hence*dp*_{n, j + 1}=*dp*_{n, j}-*dp*_{⌊ n / pj⌋, j}

Let *p*_{k} be the smallest prime greater than . Then π(*n*) = *dp*_{n, k} + *k* - 1 (by definition, the first summand accounts for all the primes not less than *k*).

If we evaluate the recurrence *dp*_{n, k} straightforwardly, all the reachable states will be of the form *dp*_{⌊ n / i⌋, j}. We can also note that if *p*_{j} and *p*_{k} are both greater than , then *dp*_{n, j} + *j* = *dp*_{n, 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

*dp*_{n, 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

*dp*_{n, 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 .

**C++ solution**

Complexity: .

Auto comment: topic has been updated by Edvard (previous revision, new revision, compare).nice editorial

This problem is also an easier version of This Problem !

That's why you were able to solve it?

Yeah!

Cool. :)

Endagorion told me that there are something very similar at Project Euler. Also the problem to count semiprimes was last year at MIPT camp (the editorial from there). Anyway I think it's very good problem for Educational Round.

Funny:

Can you explain your solution or provide some link to the theory behind it? It looks much simpler than author's.

Let

S(v,m)be the count of integers in the range2..vthat remain after sieving with all primes smaller or equal thanm. That isS(v,m)is the count of integers up tovthat are either prime or the product of primes larger thanm.S(v, p)is equal toS(v, p-1)ifpis not prime orvis smaller thanp. Otherwise^{2}(p prime, p^{2}<=v)S(v,p)can be computed fromS(v,p-1)by finding the count of integers that are removed while sieving withp. An integer is removed in this step if it is the product ofpwith another integer that has no divisor smaller thanp. This can be expressed asS(v,p)= S(v,p−1)− ( S(v/p, p−1)− S(p−1,p−1) ).During computation of

S(N,p)it is sufficient to computeS(v,p)for all positive integersvthat are representable asfloor(N/k)for some integerkand allp ≤ v.^{1/2}NOTE: Pi(N) = S(N,N).When you callS(N,N)it will need to computeS(N/k,N/k)in its lower substate. Hence you can have all required values ofPi(N/k).In my code

DSUM(N,P)do the job of calculatingS(N,P).I used two arrays

H[]andL[]for storing the values ofS(N/k,p)inH[k]fork<=Nand for^{1/2}k>=NI stored the values of^{1/2}S(N/k,p)inL[N/k].For computation I started with

p=2and changed the values of all the state which is going to be affected by this particular prime. Continue doing this for all prime tillNand at the end for^{1/2}k<=N,^{1/2}H[k]will contain the values ofPi(N/k)andL[k]will contain the values ofPi(k).PS: My complexity is

O(N.^{3/4})Idea: This Post!

Can I get your code please ???

Can you explain why the time complexity of your solution is

O(N^{3 / 4})(I find it difficult to calculate it)

Let .

Notice that

vcould only beior , where 1 ≤i≤m, and for eachS(v,v) we would enumerate values to calculate it. So the time complexity of the above algorithm isThe former part is always smaller than the latter, so we could only analysis the complexity of the latter part, which would be same order as

and it's $O(n^{3/4})$ .

By the way, if you preprocess the π(

m) form≤n^{2 / 3}, you could find that the integral becomesand it's $O(n^{2/3})$ . :)

I got AC there with the simple modification of the solution described in editorial:

Is it possible to find these materials?

I think he means to refer to this http://www.ams.org/mcom/1996-65-213/S0025-5718-96-00674-6/S0025-5718-96-00674-6.pdf

In problem F, how the recurrence dp(n, j) = dp(n, j + 1) + dp(⌊ n / pj⌋, j) is being derived?

Here's what i think:

dp(n,j) counts all positive integers

kwith the properties:k≤nk=r_{1}^{a1}r_{2}^{a2}...r_{v}^{av}, wherer_{1},r_{2}, ... ,r_{v}primes ≥p_{j}.Now we can split this set into 2 disjoint subsets:

ALL biggerthanp_{j}. This subset has exactlydp(n,j+1)elements.don't satisfy the previous property. Those are numbers in [1,n] that containat least onceIfp_{j}as a factor and all of their other factors are at leastp_{j}as well.k_{2}is such a number then it takes the form:k_{2}=p_{j}^{a}r_{1}^{a1}r_{2}^{a2}...r_{v}^{av}=p_{j}*k_{3},where

p_{j}is the j-th prime number andr_{1},r_{2}, ...,r_{v}are all primes bigger than or equal top_{j}.k_{3}is a number obviously in the range [1...⌊n/p_{j}⌋] with every factor in its PPF (Prime Power Factorization) being at leastp_{j}.There are dp(⌊n/p_{j}⌋,j) such numbers,so the recursion is correct.In problem D, "Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it ". Is there exists any case where (possibly all) all elements will be removed,i can`t fix it..

No.

What is the best approach for B if the constraints were higher (10^5 instead of 100 ) ?

I have only one idea to optimise it to O(n*k*log(m)) using segment tree.

using penwick tree , we can solve O(n*m*log(k+n*m))

first we set the value Position[i] = position

a1 , a2 ,a3 ,.... an

Position[a1]= n*m+1, Position[a2]= n*m+2,... Position[ak] = n*m+k

and make variable pos = n*m;

and initialize penwick tree using Position[i] .

if we find item X, then add penwick_query(Position[x]) to return value.

and decrease_penwick(Position[x],-1) , Position[x]= pos--, penwick_increase(Position[x],1);

above action's time complexity is O(log(n*m+k) ).

do you mean fenwick tree?

also you can write an O(n*m*log(k)) solution with ordered set.

http://codeforces.com/contest/665/submission/19748159

In problem F, can someone explain the two-step process of evaluating the DP States better?

Can someone explain me the condition cur+2^i >=k in problem E?

A question regarding problem E. When I was solving the problem, instead of writing

`int b = (x >> i) & 1;`

, I wrote`int b = x & (1 << i)`

. This change gave me AC (`i`

was in the same bounds as in the c++ solution).What's the difference?

Good day to you :)

It returns different value:

`int b = (x >> i) & 1;`

Returns 1, if ith bit is 1`int b = x & (1 << i);`

Returns 2^i, if ith bit is 1Hard to say, what happened next (for futher investigation, more code would be needed)

Hope it helped a bit ~ Good Luck :)

can anyone plz explain D in detail !

unable to grab it!

thnxxxx

1.If there's multiple ones in original set, we should choose them, because 1+1=2, 2 is a prime number.

2.Now consider what are the extra elements we should insert into the answer. I donate the answer is {1,1,....,1,A,B,C,....}, look at the {1,A,B}, there are three elements, each of them must be odd or even, so there must be two element have the same parity, so add them together will get a even number larger than 2, that is definitely not a prime number.

3.So you see, if we exclude all the ones in the answer, the answer size must less or equal to 2.

ans(Problem E) == (n * (n + 1) / 2) — ans(http://www.spoj.com/problems/SUBXOR/)

Hey guys, I use the DFS to solve problem D. I'm confused why my code passed test 11 only used 78ms where n equals to 1000, but get a TLE at test 12? here's my solution:code

Thanks

For E: Can someone please explain why we have to flip the bit b in function get()?

Thank you.

Can someone explain what is RSQ structure? It is mensioned in problem F. Had no idea what it is. Thanks.

Google "Range Sum Query", "Fenwick tree", "Segment Tree".

Can anyone explain the editorial of Problem E in more detail?

There is also another approach that I thought of: But it's a bit slow.

For all the numbers till n, we will check the divisors for the ith number till its square root — and if a number has exactly two divisors till square root of the number i.e √i. Then it will have exactly four divisors. Note: Excluding the perfect square numbers

~~~~~ Your code here...

Could somebody explain me in problem E, why do we have to go down to the subtree (b xor 1) when (cur + 2^i) < k ?

In problem E , L can equal to R . So I think we should check each elements of the array if it >= k

mạnh

xàm

Now we can simply solve the problem F with min_25 sieve in $$$O(\frac{n^{\frac{3}{4}}}{\log n} + \sqrt n)$$$ !!

I sincerely agree with you. :)

And notice that you need to calculate $$$\pi(\lfloor\frac n x\rfloor)$$$, choose Meissel-Lehmer and combine with min-25 can you reach a more efficient solution.

Can anyone please explain the logic behind the query of the trie in E?

Also does anyone know of a good tutorial for the non recursive implementation of a trie?