### AlFlen's blog

By AlFlen, 7 months ago, translation, Idea: AlFlen, 74TrAkToR

Tutorial
Solution (74TrAkToR)

Idea: AlFlen

Tutorial
Solution (74TrAkToR)

Idea: 74TrAkToR

Tutorial
Solution (74TrAkToR)

Idea: 74TrAkToR

Tutorial
Solution (74TrAkToR)

Idea: AlFlen

Tutorial
Solution (74TrAkToR)

Idea: 74TrAkToR

Tutorial
Solution (74TrAkToR) Tutorial of Codeforces Round #705 (Div. 2) Comments (152)
 » This fast :0
•  » » Video Editorial For problem C- K-Beautiful Strings: https://youtu.be/bCeBtoho8II
•  » » FastForces :)
 » Why did 109265569 give Wrong answer for problem D. TLE would be given if time limit had exceeded. But why wrong answer?
•  » » 7 months ago, # ^ | ← Rev. 2 →   Numbers won't fit in the desired data type. We are multiplying each element by x in each query. Suppose x is like 2 * 10^5, even in 100 queries, x would become 10^105 which is so large.
•  » » » So how to solve that ?
•  » » » We just need to save the factors of x, they and their times are less than or equal to 2 * 10 ^ 5
 » Does Segment Tree + bignumber work for D?
•  » » Most likely you'll get TLE. The numbers can get huge if for example we have 1 number and multiply it by 2e5 every time.
•  » » i tried to use segment tree but it gave tle :(
•  » » » i tried using seg tree but it gives WA. i used this approach https://codeforces.com/contest/1493/submission/109260301
•  » » » » 7 months ago, # ^ | ← Rev. 2 →   Modifying it (segment tree) correctly can give AC. See this 109274205
•  » » » » » Thanks
•  » » » » » why storing factors in map ? we can not use gcd function? why this logic is not working ? https://codeforces.com/contest/1493/submission/109252075)
•  » » » » » » 7 months ago, # ^ | ← Rev. 2 →   gcd of a set of numbers is the number consisting of the intersection of the prime numbers of each number in the set. You just need to take the modulo remainder of any number from the set to ruin everything
•  » » » » » » » For example a = 10, b = 5, c = 35, mod = 4. GCD(a, b) = 5, 5 mod 4 = 1. gcd(1, 35) = 1. But, gcd(a, b, c) = 5. You can use segment tree, but you need to think in another key.
•  » » » » » » » » It was a bad example, sorry. Correct example: a = 50, b = 25, c = 100, mod = 9. gcd(50, 25) = 25. 25 mod 9 = 7. gcd(7, 100) = 1. But, gcd(a, b, c) = 25. 25 mod 9 = 7.
•  » » » » » Can you explain, what exactly "addl" and "addr" is doing?
•  » » » » » » The map in each node of the tree contains the excess counts of various primes (i.e. information pertaining to which — left or right child of that node has excess). Now in order to update a node, we need to inform the node whether the left child has its gcd updated or the right child. This is communicated by calling addl() when the left child's gcd is multiplied by some factor (similarly addr()).Comprehensively, say some node's gcd has been multiplied by x, and it happens to be a left child. In this case, its parent will call addl passing x. addl will then factorise x and use these factors and the map to find the additional factors that the right child had which can be combined and used to updated the gcd here. The state of the map is also updated here. addr performs a similar task.
•  » » 7 months ago, # ^ | ← Rev. 2 →   a [i] can be equal to 10 ^ (10 ^ 5) and __gcd work in log (n). obviously it will be TLE
•  » » » Can you analyse my code's time complexity which got tle on pretest 4!!
•  » » » » Each time you run over the common divisor of the sons of the vertex. but there can be a lot of them. -> This is to add time log (x)log (n) from tree, log (x) from multiset, and the number of common prime divisors of sons-> log (x) general asymptotics O (qlog (X) Log (x) log (n)) is TLE ~ 11sec
•  » » Bignumber will definitely TLE, because operations with bigint -> +-/* work in polynomial time relative to the length of the number.
•  » » I got an AC, 109274205, while using a segment tree (modified) which ran in <650ms. The idea was to have for each segment [l,r] store the gcd of the range and a map storing for each extra prime (which is in excess in either the left or right range) its frequency (negative or positive depending on which range has excess).Now, in order to update a tree node (query), you can prime factorize the value and compare prime factors with elements of the map, and update them along with the gcd. Can someone calculate a tight bound on the time complexity, as according to me its O(n log^3 n)? [log^3 -> at each level prime factorize and for each prime factor update a map value]
•  » » 7 months ago, # ^ | ← Rev. 4 →   I think use SegmentTree to solve D is easier. 109248519.We use a dynamic create point segment tree for each prime factor to maintain single point addition and global $\min$.Regarding how to update the answer, every time we add a single point to the segment tree of a certain prime factor and see if the global $\min$ of this segment tree has changed, just fastpower the answer to multiply if it changes.$2 \times 3 \times 5 \times 7 \times 11 \times 13> 2 \times 10^5$, so we just modify at most 6 segment trees for a number change, so the time and space complexity are both It is $\mathcal O(6\times(n+q)\log a_i)$.Emm... I used $\mathcal O((n+q)\sqrt {a_i})$ prime factor，you can use $\mathcal O(n - (n+q)\log a_i)$ :P
•  » » » How is it easier? Instead of segment tree you can use multiset (as in editorial) since you only need the minimum of the full range everytime. Am I missing something?
•  » » This comment should not be downvoted, it is a friendly and kind discussion.
 » Wow, This much fast.. :)
 » The editorial came out really fast, excellent!I think this contest is as hard as I expected, and I only solve the first three problems.:(I hope I won't get any fst. ples!
 » A very great contest.Let me know how silly I am.E is very easy but I wrote a very long and incorrect code :(
 » Why you just don't give transformations in B
•  » » n problem B there is one test case 51 3 30:01 (8th case) How is answer 50:00? shouldn't the answer be 00:00? as if 50:00 then 00:05 isn't a valid time...plz explain someonehttps://codeforces.com/contest/1493/submission/109793069 here is my submission Can you plz help me out? is the jury's answer wrong?
•  » » » You forgot to consider the fact that mirror image of 5 will be 2, So this is valid time.
 » Not so easy round! xD
 » Such a FAST update! It really helps me because I can't wait to learn the solutions of the problems!
 » 7 months ago, # | ← Rev. 3 →   Waiting for another contest from you :)) !
•  » » Implementation heavy contest. But I think questions were not that much tough.
 » I hastily read the problem statement for C and solved it for exactly k occurrences. Feel so dumb now. Also this contest seemed more on the implementation side.
 » Can someone explain how to estimate the time complexity of D?
•  » » 7 months ago, # ^ | ← Rev. 4 →   (n + q)log(max(X)) we can get answer for each request in log (x) using EratosthenUPD: i missed log from mutiset
•  » » » multiset factor is missing
 » 7 months ago, # | ← Rev. 3 →   Thanks for contest
 » So fast
 » Thank you for the contest!
 » I had a slightly different approach to D which didn't require the use of multiset, first we calculate the gcd after processing all the queries and after that we process the queries in reverse order where instead of multiplying we divide the index with the number given and if the number occurrences of a prime factor becomes less than the number of occurrences in the current answer we change the answer accordingly.
 » There were some difficult problems but still a great, well put together round after all!
 » Nice round and fast editorial
 » Anyone having tough times understanding C?
•  » » Check this: https://codeforces.com/edu/course/2/lesson/2
 » implementForces :(
 » very fast editorial!!!!!
 » The solutions came out fast!!!
 » I made the logic of C problem in approx 30 minutes, but for implementation It took totally 1 hour, but the logic was correct. due to one silly mistake not able to get ac. :(
 » can someone explain d?why answer wont dec it can be 1 also sometimes..
•  » » 7 months ago, # ^ | ← Rev. 2 →   GCD is really the smallest power of each prime multiplied together. So after multiplying a value x, for each prime, its power is not going to decrease.
 » 7 months ago, # | ← Rev. 2 →   I do not think this is true for problem C: "If the string s is beautiful, then s itself is the answer." Because the length of s can be smaller than n as I it does not state otherwise in the problem statement. Otherwise a great tutorial. I had the exact same idea but took me too long to implement it sadly:(
•  » » length of s is always n, as it says in the input section of the statement
•  » » » Indeed. Sorry then. Was a great contest!
 » probelm B: https://codeforces.com/contest/1493/submission/109275855 Im not able to figure out the mistake.
•  » » 7 months ago, # ^ | ← Rev. 3 →   You are setting "a" to 2 and two lines below you are setting the same 2 to 5. Same for var b. Those if should have been if-else. I don't know if there are more problems, but at least that is.
 » I used segment tree in D and it worked well. Just add new nodes dynamically so as not to get a TLE.
 » I cannot get the mistake in my Problem C's code. (WA on test 3 (272th line). Could someone help?Code link
•  » » Maybe you need to increase the amount of occurences of 'a' on suffix to make sure that the length of answer equals n instead of adding the letter *st.begin() .
•  » » » Thank you, now it is accepted as that was the only error.
 » Thank you for fast tutorial!
 » According to the editorial, I think it would be better if you used -1 for not calculated states. Using dp=2 is a little bit confusing, because as you said in the editorial, number of r is just the sum of dp... and etc. Just some advice.
 » QDuntil now i don't know why segment tree got MLE, i think because of recursionhere is my impl. if you wanna chick ithttps://codeforces.com/contest/1493/submission/109276549
 » Why this logic is not working in problem D can anyone tell me please? Link : https://codeforces.com/contest/1493/submission/109252075
•  » » 7 months ago, # ^ | ← Rev. 2 →   You can't take modulo like thatFor example if (mod = 3)array = {4,6,8}, gcd(array) = 2then if we get modulo for themarray = {1,0,2}, gcd(array) = 1
•  » » » ok thank you
•  » » » If we have only two numbers than we can take module separately for gcd??
 » Am getting Runtime Error on TC 4 Submission Link: https://codeforces.com/contest/1493/submission/109286557 I have taken the variable names as intuitive as possible in case you decide to help me. Please tell me why am I getting this error, what mistake have i made in my code.Thanks in Advance !!
•  » » // find and update in set auto it = factor_to_freq_index[factor].find({prev_freq, i}); if (it != factor_to_freq_index[factor].end()) factor_to_freq_index[factor].erase(it); I am not particularly sure, but in a rough glance this seems to stand out. If previously there were no factors for $a_i$, prev_freq will be $0$, and you will be erasing some random element from the factors frequency set.
•  » » » Hi, i don't think thats an issue,coz:- Am storing for each prime factor its power/frequency for all indexs in factor_to_freq_index map so all elements will be distinct.ith factor -> has elements {power of ith factor on jth index ,jth index}I hope this makes it clear. Anyways, thanks for trying and please do let me know if incase you find any issue.
 » 7 months ago, # | ← Rev. 2 →   Problem F: you can check if $k$ is a period in $3$ queries.This is equivalent to check if $x = n/k$ "blocks" of $k$ rows are equal.Let $[l, r]$ be the segment of blocks from $l$ to $r$. Let $y = \lfloor (x-1)/2 \rfloor$.It's enough to check if $[1, y] = [y + 1, 2y]$ $[1, y] = [y + 2, 2y + 1]$ either $x$ is odd, or $[1, x/2] = [x/2 + 1, x]$ This was enough to get AC with some dirty randomization. 109275723
•  » » I also used kind of the same approach, but without randomization it also passed. 109267700. I tested my approach locally against all pairs of $r,c \leq 1000$ with the worst case for my program (when all queries return 1, all elements are equal). For all r and c my approach was under the query limit.
•  » » You need to cut only by primes. And if cut fails don't try same prime again. 109297822I did check that n blocks are same: if n == 2 just compare 0 and 1, otherwise if n is prime then it's odd, so: check [0,(n-1)/2-1] = [(n-1)/2,n-2] check [1,(n-1)/2] = [(n-1)/2+1,n-1] check [0,1] = [1,0] if necessary (now it looks like ababababa, and with n = 3 you already know a = b)
•  » » » 7 months ago, # ^ | ← Rev. 2 →   Regarding to number of checks. Factorization of N has at most $\log_2(N)$ factors, for 2 it performs 1 check, for 3 it performs 2 checks, each factor >= 5 it performs 3 checks. For any M with prime factors >= 5 there is N < M with prime factors replaced into 5 with same worst case checks amount. In other words, worst checks count is maximum for some number $N = 2^a3^b5^c$, then checks $a+2b+3c$ and $\log_2(N) = a+b\log_2(3)+c\log_2(5)$. So, we want to maximize $a+2b+3c$ over constraint $a+b\log_2(3)+c\log_2(5) \leq \log_2(N). a, b, c >= 0.$ We can apply linear programming knowledge. For real values a,b,c maximum is bigger than any integer solution. Vertices gives $\log_2(N)$, $2\log_2(N)/\log_2(3)$, $3\log_2(N)/\log_2(5).$ So, winner is $\log_2(N)\cdot 3/\log_2(5)$ which is approximately $1.29203\cdot \log_2(N)$.
•  » » » » 7 months ago, # ^ | ← Rev. 2 →   You can also do this: We only do one dimension of the grid at a time. Call this dimension n. Then the prime factorization of $n = p_0^{k_0}\cdot p_1^{k_1} \dots$. Do queries for each distinct prime factor. So for $p_i^{k_i}$ We need to find the biggest $c \leq k_i$ such that the grid can be divided in $p_i^c$ blocks. We do this incrementally. We can check first if the grid is divisible by p blocks. Than we check if the first out of these blocks is again divisible by p. We do this procedure recursively c times. For odd primes we can do this in 2 queries per divisibility check. For the only even prime $2$ we only have to do one query. So the worst case queries is # factors of 2 + 2* #factors of all other primes. The worst case is either $log_2(n)$ queries or $2 * log_3(n)$, the smallest odd prime. So the worst case for one dimension for this approach is $\frac{2}{log_2(3)} log(n) \approx 1.262 log(n)$
•  » » » » » how can you check with two queries for odd primes >= 5?
•  » » » » » » oh, I see now. it's above. [1,y]=[y+1,2y], [1,y]=[y+2,2y+1].
•  » » I think $2$ queries are enough. If $x$ is odd, check the first two points and if it's even, check the first and the last points. Here's a submission using $2$ queries.
•  » » 7 months ago, # ^ | ← Rev. 3 →   I can check if k is a period in 2 queries. if x = [n/k] "blocks" and x2 = [x/2] I call {L,R} is blocks from L to R if x odd you will check: {1,x2} = {x2 + 1,2 * x2} and {1,x2} = {x2 + 2,2 * x2 + 1} if x even you check: {1,x2} = {x2 + 1,2 * x2} and {1,x2 — 1} = {x2,2 * x2 — 2} This is my code: https://codeforces.com/contest/1493/submission/109450681
 » Can anyone tell why my solution to D will not exceed the memory limit. As in my accepted solution I am creating a map of multiset of the number of each prime that I am getting. and in worst case, if a prime is present in all n elements then the size of this map of multiset will be (2*1e5)*20000 memory (as there are more than 20000 primes from 2 to 2e5), which is too huge to pass. But I am not sure how it is getting accepted?
•  » » This applies for any solution which stores only non-zero prime powers for each element. Any number less than 2e5 is made of $<18$ unique primes. So initially, we have at max $18*n$ {prime, position} pairs. In each update, we are are multiplying by $x$ which can bring at most 18 more unique {prime, position} pairs into the map. So, in the end the map should not have more than $18(n+q)$ {prime,element} pairs, which is $O((n+q)log(max))$ memory complexity.
 » What should be the result for problem B for the following input1 51 3 30:01The official tests say it should be 50:00 whose mirror image is 00:05. 00:05 is invalid since our minutes are only till 3
•  » » I think I got it. The mirror image of 50 is 02.
 » 7 months ago, # | ← Rev. 3 →   Another way of proof 1493E - Enormous XOR. very long proofLet's say f(x) is XOR of all numbers in range [0, x] inclusive. Then XOR of all numbers within range [a, b] is f(b) XOR f(a-1) — idea is similar to prefix sum, so I usually call it prefix XORs. Then, easy to show that f(b) is: b if b mod 4 = 0 1 if b mod 4 = 1 b + 1 if b mod 4 = 2 0 if b mod 4 = 3 Then, also easy to show that f(a-1) is: a-1 if a mod 4 = 1 1 if a mod 4 = 2 a if a mod 4 = 3 0 if a mod 4 = 0 So, now we can see that our answer can be XOR of any of 16 combinations. Instead of considering each combination, it's easy to see that values can be 0, 1 or within range [l-1, r+1]. We also always can get r if we pick only number r. This naturally leads into question: when can we get f(b) XOR f(a-1) bigger than r?Now, crucial observation is following: let say we XOR two numbers from range [l-1, r+1] and both l-1 and r+1 numbers starts with 1. (highest bit of number l-1 is 1, and highest bit of number r-1 is 1) Then all numbers within range [l-1, r+1] has highest bit 1 in its binary representation. Then any x XOR y within the range gives 0 in highest bit. In this case we know that any case with f(a-1) = a or f(a-1) = a-1, f(b) = b, f(b) = b+1 are useless because their XOR is less than r. In that case, one of value should be 0 or 1.If one of values is 0, then 0 XOR v is just v. So, maximum is when v is maximum. Among 0,1,[l-1,r+1] maximum is r+1. And we already know how to get f(b) XOR f(a-1) = r. The value r+1 is possible only as f(b) = b+1 = r+1. In other words, when b = r. Also it require r mod 4 = 2. Then, we want f(a-1) = 0, and it is possible when a mod 4 = 0. If r mod 4 = 2 then (r — 2) mod 4 = 0, so f(r) XOR f(r-2) = r + 1 in this case. In other words, if r mod 4 = 2 and we can pick range [r-2, r], then we get r + 1. Otherwise, either r mod 4 != 2 and we can't get r + 1 value, or we can't get a mod 4 = 0 because l > r-2.If one of values is 1, then 1 XOR v is v — 1 if v is odd, and v + 1 otherwise. We want v XOR 1 > r, this means either v is odd and v — 1 > r, thus v > r + 1, which is impossible; either v is even and v + 1 > r, thus v > r — 1, so v >= r.Case v = r. f(b) = b = r and f(a-1) = 1. b mod 4 = 0, a mod 4 = 2, in other words r mod 4 = 0 and r — 2 >= l so we can pick [r-2, r]. f(b) = b + 1 = r and f(a-1) = 1. b mod 4 = 2, in other words b = r — 1 and r — 1 mod 4 = 2. So r mod 4 = 3 which means r is odd, but we require r = v to be even, so this case is impossible. f(b) = 1 and f(a-1) = a-1 = r. a-1 = r is impossible because a <= r. f(b) = 1 and f(a-1) = a = r. b mod 4 = 1 and a mod 4 = 3. And, because a <= b <= r means b = r, but r mod 4 can't be equal to 1 and 3 at the same time. So this case is also impossible. Case v = r + 1. This only possible when f(b) = b+1 = r+1 and f(a-1) = 1. b mod 4 = 2 and a mod 4 = 2. Here b = r, and r mod 4 = 2, so r + 1 mod 4 = 3 in other words v mod 4 = 3, which means v is odd. So, this case is also impossible.Summary: if highest bit of all numbers within range [l-1,r+1] is 1, then we can always get r. If r is even we can try pick [r-2, r] and in case r mod 4 = 2 we get 0 XOR r+1 = r+1, or in case r mod 4 = 0 we also get r XOR 1 = r+1.You can avoid checking cases above analytically, you can just check all allowed [a,b] where b in range [r-3,r] and a in range[b-3,a]. Because maximum of 0 XOR v or 1 XOR v is obviously reached only there. Now, what happens when [l-1, r+1] has highest bits 0 and 1? First, case: when l and r has different highest bits, then we can get [01111....,10....] as [a,b] and get XOR = 111111... Otherwise [l,r] all has same highest bit.Case highest bit of [l,r] is 0, then it doesn't satisfy conditions of the problem. In statement highest bit of r is 1 unless r = 0. Case r = 0 is trivial. Case highest bit of [l,r] is 1, then l-1 = 011111.... or r = 1 (trivial). We considered all cases when x XOR y highest bit both set, and also 0 XOR y and 1 XOR y. So cases we didn't check here is case 0111111.... XOR y, 011111... XOR 0, 011111.... XOR 1. But this require f(x) = 0111111.... = l-1. This is only possible when f(a-1) = a-1 = l-1. And this require a mod 4 = 1. But obviously l — 1 mod 2 = 1. This means we can't get f(x) = 011111... So here answer is also r or r+1 depending on parity of r.
 » Those who tried to solve B in contest we missed to convert 2 to 5,5 to 2
 » https://codeforces.com/contest/1493/submission/109287327Problem — D Please tell me, where I am getting wrong. I have used a segment tree to solve this problem.Thanks in advance!
•  » » 7 months ago, # ^ | ← Rev. 2 →   Your solution is wrong, because you can't just take modulo. For example, if $n=2$ and $a = a = 2*10^5$, for query $i=1$ and $x=2*10^{5}$, you will multiply first number of array by $2*10^{5}$ and take modulo $10^{9}+7$, that's wrong, because $gcd(4*10^{10} \, mod \, (10^{9}+7),\, 2*10^{5}) = 1$ is not equal to $gcd(4*10^{10},\, 2*10^{5}) = 2*10^{5}$.
•  » » » Can you please tell me then how should I overcome this situation, here?
 » In the editorial of problem B, I think the context should be"you need to look over all the moments of time from the given one"
 » 7 months ago, # | ← Rev. 2 →   Could someone please tell why my submission in problem D giving TLE , I have used same idea as given in editorial . 109312305 .Edit : I figured out , i was making mistake in calculating the sieve.
 » Video Editorial For problem C- K-Beautiful Strings: https://youtu.be/bCeBtoho8II
 » Thank you for the editorial :)
 » 7 months ago, # | ← Rev. 2 →   Can someone explain why does the solution given in editorial for D does not exceed memory limit? In the multiset cnt, every i from 2 to maxn can be of size n, therefore memory required will be O(n^2), which will be too much. Am I missing something?
•  » » the number of prime factors of n is bounded by $O(logn)$ ,each number can only increase the size of cnt by $logn$ ,in total you process n numbers from the initial array and q queries so the size of the multiset is bounded by $O((n+q)*log(2e5))$
 » How is Problem C implemented with binary search? There is the tag.
 » Please tell me why I get time limit exceeded with this ? https://codeforces.com/contest/1493/submission/109323707
•  » » in the update, you run through the divisors of sons every time. but there can be A LOT of them -> from this you get TLE.
 » May I ask if there are some problems in problem F's example?Through the Q & A in the example we can not sure that the matrix is like that...What if the matrix is 1 2 1 2 1 2 1 2 1 2 1 2 Then the answer will be $2 \times 3 = 6$ and not $2$.
•  » » Sorry I get it...The example needn't to prove the matrix is like thisIt only needs to let the answers of the asks conform to this matrix... » This contest is very helpful for me and Editorial is also very nice !!
 » In the editorial solution for C, is there a specific reason for not just using 'return' incase the given string is already beautiful or the case when no beautiful string is possible? They used 'continue' and a flag to skip the 'for loop'.
 » 7 months ago, # | ← Rev. 2 →   In question C (K-beautiful strings), I had a doubt in the tutorial statement:"If sum
•  » » Overall length $n$ is a multiple of $k$. Every letter appears some multiple of $k$ times in the the first $pref + 1$ and the last $sum$ positions combined (because of the way we are constructing the suffix). Hence the remaining number of positions should also be a multiple of $k$, which we fill all with as.
•  » » » Yes I get it now. It was quite a simple thing, I just over complicated it. Thanks for help!
 » 7 months ago, # | ← Rev. 2 →   Can someone please tell why my code for C is giving Memory limit exceeded on test case 3 Link to the code: https://codeforces.com/contest/1493/submission/109341765 I have done according to the logic in editorial.GOT IT :)
 » Fuck you AlFlen. it's one of the badest contest
•  » » No need to curse ;(I can't even see round 705 in your contest list.Can you elaborate why is that round one of the "badest contest"?
•  » » Don't personal attack plz...
 » Could someone give me a hint on why the upper bound on the query count is true for 1493F - Enchanted Matrix given that I use the strategy in the tutorial?
•  » » The editorial was updated, now it contains a more accurate upper bound and a quick proof
•  » » » Thanks!
 » 7 months ago, # | ← Rev. 2 →   Can someone clarify the below statement **__gcd(a, b) % c == __gcd(a % c, b % c) % c** ?
•  » » this is generally a wrong statement
 » 7 months ago, # | ← Rev. 2 →   Concerning the tutorial to Problem C:"A detail of realization: we will consider iterated letter at position pref+1 in the array cnt."I might be wrong but I don't think this sentence is correct. The Russian version seems to be on point though.
 » 7 months ago, # | ← Rev. 3 →   Haven't read the editorial's proof of E (because I'm too lazy :D) but I found a simple one which is probably different, so here's a (very) informal proof:Suppose that $n \geq 2$ and that we're dealing with the "interesting" case (the most significant $1$ bit is shared in $l$ and $r$.) We can immediately use $r$ as a lower bound for $f(l, r)$. Note that we must choose $x$ and $y$ with the same parity, or else $g(x, y) < r \leq f(l, r)$ (because there'd be an even number of most significant bits). Therefore, their binary representations must either be $(...1, ...1)$ or $(...0, ...0)$. In the first case, all of the bits in $g(x, y)$, except the least significant one, are the same as those in $x$ (by a symmetry argument).In the second case, all of the bits in $g(x, y)$, except the least significant one, are the same as those in $y$ (by a similar argument).In either case, the substring consisting of these bits in $g(x, y)$ is lexicographically smaller than or equal to the same substring in $r$. Therefore, we can always set these bits in $x$ or $y$ (depending on case) to be equal to those bits in $r$ (let's call an assignment of $x$ and $y$ which satisfies this as the bruh condition). So what's left is determining if we can make the one bit of $g(x, y)$ to be on or not.If $r$ is odd, we can make it on by setting $x = y = r$ (this is definitely maximal because it satisfies the bruh condition and has the one bit set).If $r$ is even, we must set $y = r$, or else the bruh condition wouldn't be satisfied. If possible, setting $x = r - 2$ is maximal because of the same reason as the odd $r$ case. Otherwise, it's easy to see that setting $x = y = r$ is the best we can do.
•  » » 6 months ago, # ^ | ← Rev. 5 →   Thanks.I don't understand all of the non-one bits at first, but soon i got the key ideas and solved it. My explanation of the proof: If both $x$ and $y$ are odd ($...1$), then all of the bits(except the least significant bit) in $g(x,y)$ are the same as $x$, so $g(x,y) = [x-1, x]$. That's because $g(x,y) = x \oplus (x+1 \oplus x+2) \oplus (x+3 \oplus x+4) \dots$ and all of the bits(except the last bit) in $(x+2k-1,x+2k)$ are the same. If both $x$ and $y$ are even ($...0$), then all of the bits(except the least significant bit) in $g(x,y)$ are the same as $y$, so $g(x,y) = [y, y+1]$. The proof is similar. So for every $y$, the maximal value of $g(x,y)$ is $y$ (if $y$ is odd) or $y+1$ (if $y$ is even). If $r$ is even and $r - l \geq 2$, then the answer is $r + 1$, and it's the maximal possible answer; Otherwise, the answer is $r$, because $\max(g(\dots,r)) = r$(because $r$ is odd or $r - l < 2$) and $\max(g(\dots,l\sim r-1)) \leq r$.
•  » » » ah, despite my best efforts, I now realize that "all of the non-one bits" was terribly phrased LOLglad that that actually helped someone tho!
•  » » » Great proof and explanation! Thank you.
 » Problem C: Test 3:wrong answer There is a nice line smaller than the given answer (test case 272) Can anyone tell me "test 3 case 272"
•  » »
 » Never thought passing an ArrayList to a function can be taking so much time. Costed me TLE and hell lot of time to debugjust instead of passing Arraylist passed the same array
 » 7 months ago, # | ← Rev. 3 →   Another (easier?) way to prove E: note that $4k \oplus 4k+1 \oplus 4k+2 \oplus 4k+3$ is $0$. So there are atmost 6 "alive" terms in XOR of any range (a prefix of length atmost $3$ and a suffix of length atmost $3$ ).Your 4 paragraph proof of E comes down to two lines if you just observe this fact :P
 » 7 months ago, # | ← Rev. 2 →   Shock! A Candidate Master rank 8000+ but gained +77 rating points and became MASTER!!1Standings , and you can see rating changes => LYC_music's ProfileNeed I to @ Mike ?
•  » » 7 months ago, # ^ | ← Rev. 4 →   .
•  » » Because these two competitors cheated in this contest and the rating have not rolled back yet.
•  » » Maybe it will roll back soon...?
•  » » » 6 months ago, # ^ | ← Rev. 3 →   After four days. I think it's a bug in website, or website maintainer forget to change rating.
•  » » I think cheating people's ratings should be lower instead of just cancel the rating changes of this contest.
 » Can someone explain to me why (in the solution for problem C) the suffix receives a letter a until it has the appropriate size? Couldn't that make the number of occurrences of a not divisible by k?
•  » » Can I get the full test data of question B and C ?qaq... Can not find where I get wrong...
•  » » As n and (pref + 1 + suff) are both multiples of k, the difference n — (pref + 1 + suff) is also multiple of k, so fill the gap with these additional pref 'a' won't influence the correctness.
 » I think the solution code of question C may be wrong. I use following data to test: 2 20 4 bacefbbcceafddeacbeacabce 21 3 bacefbbcceafddeacbeacabcacorrect answer may be: bacefbbcceafeaabceff bacefbbcceafddeacccdfbut it gives the result as: bacefbbcceafeaacccff bacefbbcceafeaaabbccfthe number of some letter's occurences can't be divided by k
•  » » The length of string s is n
•  » » » Thanks, let me figure it out how it exactly work...
 » 6 months ago, # | ← Rev. 3 →   Is it possible to solve problem_D with Segment tree?
•  » » Yes
 » AlFlen In tutorial of problem F, the last paragraph The worst case is when we divide current minimal r by 3 with 2 queries. I think the worst case should be when r is divided by 5 with 3 queries, because $3 / \log_2 5 > 2 / \log_2 3$.