CodeTON Round 3 (Div. 1 + Div. 2) Editorial
source link: http://codeforces.com/blog/entry/108504
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
A — Indirect Sort
Authors: mejiamejia, Ecrade_
B — Maximum Substring
Author: tibinyte
C — Complementary XOR
Author: tibinyte
D — Count GCD
Author: tibinyte
E — Bracket Cost
Author: tibinyte
F — Majority
Author: tibinyte
Challenge: Solve the problem in or ( modulo is prime )
G — Doping
Author: tibinyte
H — BinaryStringForces
Author: tibinyte
#LeafTON
17 hours ago, # | The Solution code is not revealed |
17 hours ago, # | tibinyte orz |
17 hours ago, # | omg green editorial |
Great contest! Seems like the editorial was prepared before the contest :D |
I use divide and conquer to solve E. Assume that
Using this, we may just compute sum of and of for every sub-string separately by splitting a string in two equal halves, computing answers on them recursively and then computing answers for strings passing the middle point by sorting prefixes of right half by and by , and by using some prefix sums on them. |
-
can you explain more that how you got ans for merged string str1+str2 from answers of str1 and str2 and t1 t2 m1 m2 ,ok that answer of this string is |m| + max(0,t) but how to get answers of all substrings that pass from the concatenation point of str1 and str2
-
Balance of the concatenation is just the sum of their balances. And minimum prefix balance is either in the left half, in which case it's or in the right half, in which case it's . You just pick the minimum.
To compute contribution, note and:
- Sort right half by ;
- For each possible , find first such that ;
- For elements to the right of it, contribution is and to the left it's .
To compute contribution, note and:
- Sort right half by ;
- For each possible , find first such that ;
- For elements to the right of it, contribution is and to the left it's .
Actual contributions then may be computed with prefix sums. My sol is 179624832. Now that I think about it, my complexity is actually because of sorting... Though one could've used counting sort instead.
-
17 hours ago, # | Can anyone explain in problem D, how to use the inclusion-exclusion principle to find the count of numbers in range [1, m / a[i]] that are co-prime to a[i — 1] / a[i] ? That's essentially what we are trying to do for each i, right? To find out the count of numbers that are co-prime to a given number x in a given range starting from 1. |
-
Let the prime factors of
a[i - 1]/a[i]
bep1, p2, ..., pk
. The goal is to compute the number of numbers in range[1, m / a[i]]
such that they are not divisible by any ofpi
. To do this, letA_i
be the set of numbers in range[1, m / a[i]]
such that they are divisible bypi
. We can apply PIE on allA_i
to find the total number of in-range numbers that are divisible by somepi
.m / a[i]
minus this number is the desired result
17 hours ago, # | Fast editorial? orz |
17 hours ago, # | First BinaryStringForces round on codeforces.com |
-
I really couldn't make all the tasks about binary strings, but that was the intention...
I was inspired by antontrygubO_o's xor contest idea on codechef.
17 hours ago, # | Problem statements were short and quite simple to understand. No unnecessary stories. Thank you. |
16 hours ago, # | Quickest Tutorial Ever, WOW! Good Job boys .. |
My solution for D is similar but instead of factorizing a1, I use the fact that a[i]/a[i+1] is amortized: We notice that b[0] = a[0] and let's look for transition from b[i] to b[i+1]: Exist a number b[i+1] s.t. it is possible to do the i -> i+1 transition <=> gcd(a[i], a[i+1]) = a[i+1] b[i] must fulfill that gcd(a[i], b[i+1]) = a[i+1] <=> b[i+1] € {x s.t. gcd(x/a[i+1], a[i]/a[i+1]) = 1 and a[i+1]|x and x <= m} Those are the number of co-primes numbers of a[i]/a[i+1] in range [0, (m/a[i+1])] To get the number of co-primes number of a[i]/a[i+1] in range m/a[i], we can go through the factorization of a[i]/a[i+1], keep unique primes in a vector, let's call it "decomposition", and then go through all the 2^(|decomposition|) subsets with inclusion-exclusion to get the number of non-co-primes numbers of a[i]/a[i+1]. (we get the number of multiples of every subset of decomposition by (m/a[i]) / LCM(subset) and add it or subtract depending on the parity of the cardinal of the subset (this comes from inclusion-exclusion)). Considering 2 sqrt(m)/logm the number of primes in [2, sqrt(m)] we get this is O(n*(2 sqrt(m)/logm) + sqrt(m)log(m)), but it turns out that a[i]/a[i+1] is amortized and then (2 sqrt(m)/logm) is a constant, so we get O(n + sqrt(m)logm). Sorry for not using LaTex, I should definitely learn LaTex |
In problem C if all elements of both arrays are equal to 1, the editorial solution will do 2*n operations , right? wasn't it suppose to make at most n+5? or am I mistaking something? |
-
nvm I'm dumb
I want to plug my solution for Problem E here which does not use a BIT, just prefix sum, a map (which couldve been a vector, but I was too lazy for negative indices...) and two for-loops. Solution: 179633595 Explanation: Link to comment in Announcement. |
16 hours ago, # | can someone explain C logic . |
16 hours ago, # | For problem D, I am generating the prime factors of all the numbers but it is not giving me a TLE. Example: Then isn't the time complexity x and won't this give TLE? |
-
Your testcase is wrong, because you check in the very beginning, that divides
And since every next number divides the previous one, then you'll need to factor a number except 1 only at most times. So we can limit the complexity to . And an even better limit would come from the fact that the product of all the numbers we decompose is , and the fact that gives us the total complexity of
16 hours ago, # | Okay I'm a bit annoyed (and it certainly is my fault) that I FST'd Problem D and once again (yes, previous times also I was reaching Master and then FST'd) I have to wait to touch that yellow colour again. What was the mistake? At this point, it seems like my fate doesn't want me to reach master again :( Anyways, it was a great contest! A big thank you to everyone involved in the problem setting team. (Although I believe C was a bit harder than usual :P) |
-
The comment is hidden because of too negative feedback, click here to view it
16 hours ago, # | Is the runtime complexity of problem D wrong ? Not sure what is the log in 2^9 * 9 * log + sqrt(M). Shouldn't it be 2^9 * 9 * N + sqrt(M) per test case, where N is the length of the input array A's length per test case ? |
16 hours ago, # | Problems Were really nice :) |
16 hours ago, # | The links in your blogpost are a bit weird, since clicking on the caption "A -- Indirect Sort" sends you to Problem B :) But it is a great contest and editorial! |
-
when you find that ans is zero( v[i-1]/v[i] is not integer ) you can break,but if you don't the value of v[i-1]/v[i] keeps jumping ex 1 1000000000 1 1000000000 1 1000000000 1 1000000000... so so your actual complexity is now N*sqrt(m) instead of sqrt(m)
What was the point of setting non constant in problem F? I had a very funny bug in my non-static template. I forgot that the isn't always prime, so for calculating to the power of I took modulo . And due to the small constraints on I didn't find correctly and didn't get accepted ;) Anyway the problem was good in my opinion, and would be better with the constant . |
15 hours ago, # | How to make observations such as "The cost of s[l + 1, r] is max(bl, br) — min(bl, ..., br)?" |
-
I can share what I did, it may be a bit complicated.
So, let's divide all of our sequences into two types of groups — positive and negative. (positive have balance > 0, negative — <= 0) For a bracket sequence b, let's call P the absolute value of the minimum prefix balance that is achieved in it (ex, for )(()) it is 1) I claim that the cost for making a permutation good is P for negative BS's and P + balance for positives. How to prove? Let us prove at the start that for a bracket sequence with balance = 0 the cost is P. Let's take first P opening brackets and rotate them to the start — that is the construction. And given that one operation increases a given prefix value by no more than 1, this proves that less than P is impossible. Now, for positive BS's you always need at least BAL closing brackets, which you simply append (the optimality of this is trivial), and then you need P rotations. For negatives — we simply assign the required opening brackets to the start and repeat the proof. Now, you can either simply code this approach (two ST's + deque, my submission = https://codeforces.com/contest/1750/submission/179686679), or you could reorder the formula some more and get the author's one.
15 hours ago, # | A tough contest that ended my positive delta streak for 10 rounds, however it was well prepared and I learnt a lot, Thanks. Orz tibinyte |
14 hours ago, # | In editorial of C, "For each operation, the interval changed by a sequence and b sequence is complementary, so you must judge whether all [ai=bi] are the same at the beginning." Can someone explain in more detail? |
-
Maybe this will be easier:
You can see that this
will be true after every operation, so if we make every , then will hold for every i and j
-
bashkort Can you explain how to further approach the solution after this observation?
-
If initially there are i, j such as , then answer is clearly NO, because if we could make a's and b's equal to zero, then there were no i, j such as .
After we made every , then every or every , so if b's are equal to zero, then we solved the problem, or if every b is equal to 1, then we do 3 operations:
(1, 1), (2, 2), (1, 2)
-
14 hours ago, # | Can someone explain the solution to the problem F properly? |
-
Let be the number of sequences of size whose maximum electrifiable prefix is of size . For example, , corresponding to any sequence starting with , and is the solution to our problem. Also note that .
To calculate for we use a recursive formula. Such a sequence is made by a electrifiable sequence of length followed by either all zeros or enough zeros then a one. In the second case, there are at least zeros before the one, where is the maximum electrifiable prefix of the subsegment starting at the one. Thus, if is the length of that subsegment, we have , where is any amount of extra zeros in that segment. For each satisfying we have a set of sequences of size . Since we can determine in terms of and (having fixed and ), the sets given by every pair are disjoint because they either have a different number of zeros before the one or a second fully electrifiable subsegment of different size.
The condition is equivalent to . Thus, the formula is
To compute the sum we can something similar to 2d prefix sums but for a triangle instead of a square.
Code: Submission
-
If somebody still does't understand the solution for F, I hope this would be helpful.
Let's fix a binary string (assuming that the first and the last elements are ones). And let's imagine that wile we can make the following operation we make it. Of course it's always good for us. Then let's look to the final string:
It looks like . When this string could actually be final (i.e. we can't make any operation)? It's not hard to see that must holds. And it's not hard to see that if it holds then it's not possible to make an operation.
Let be the answer for the length . Then if we know and then for how many strings this string will be final? Answer: . Let's call it contribution of the final string.
Let be the sum of contributions over all final strings of length , such that (there're zeroes in the final string). Then (cause as we said we only consider strings that starts and ends with one).
To calculate we can write the following dp: let be the sum of contributions over all final strings of length that ends with zero and we can insert at most ones to it's end so it still would be final. can be negative, but .
Note that in this dp we only consider strings that ends with zero and starts with one.
To calculate there're following transitions:
+= . Here we insert one zero to the end.
For all such that balance : += . Here we just insert to the end.
If we knew we could calculate this in , because there're transitions of the first type and for second transitions we can fix and use suffix sums to make them fast.
Final step: to calculate we need to know only for . And if for all we've already calculated then we can calculate . To do this we need to iterate over the number of ones in the end of the final string and using the same suffix sums update . Knowing we can calculate . So we can calculate and at the same time.
Total time complexity: . Code
13 hours ago, # | Can someone please explain why in problem D, |
-
consider a[i-1]=2*2*3 and the current b[i]=2*3*7.
Then a[i]=gcd(a[i-1],b[i]) = 2*3 since 2*3 is their common factors.
That mean that prime factors(a[i]) should be a subset of prime factors(a[i-1]) and this mean that a[i-1]%a[i]==0.
In problem D ,I used inclusion-exclusion principle but why my code gives wrong answer in test 3 in the sample cases? |
In problem C. Why does a[i] have to be different from b[i]? In test case: Is not a valid solution: |
7 hours ago, # | For proA what for the input is the sequence "2 3 4", it has already been sorted and A[0] != 1, why it output is "No"? |
The solution of problem B can be better by the count of 0 = n — count of 1 |
Can you tell me why when respect then we can just use right bracket at the end of string? In case of ,1-Index,let . It's easy to see that 。But we should do operation 1 one time,and add two left bracket on the left. If we just work as the edtorial, we can make the last bracket be matched. Sorry for my bad English. But can anyone teach me? Thank you very much. |
5 hours ago, # | About problemC, maybe follows this logic can be understood better. Firstly, we assume every char of string a and b is '0', let we operate it randomly, we can observe that when our operates is odd the string will be , and if operates is even the string will be . thus we know only if the string or for every index i, the problem has the solution. |
For E, you don't need to use BIT to calculate the answer, you can split it into two parts (I concatenated 0 to the start of prefix sums) Which is just sum of max over all pairs in array (which you can calculate by sorting) minus the sum of minimums for all subarrays (which you can calculate using contribution technique and stacks(next and previous smaller element)) |
4 hours ago, # | Why problem 2 gave TLE for same approach written in Java. I have done both work in one for loop , still it gave me TLE. |
3 hours ago, # | Can anyone explain for C : Observe that answer is only possible if both the string is the same or if we can get b after inverting each character of a. How this stataement is true , i not able to understand |
This code is used to count the max number of continuous 0s and 1s. But it fails on some test cases in the second pretest. Can anyone provide a test case when this fails? |
I can hardly understand the editorial for Problem G. Can anybody explain it in a more detailed way? |
2 hours ago, # | I did problem B with same aproch, but I am getting TLE. Can someone tell me what am I missing? My submission: 179577559 |
93 minutes ago, # | Here is my implementation of E. It uses the same idea as the official solution, but only requires an std::set instead of a BIT. By inserting the indices to a set in sorted order of prefix sum, we can find the possible starting and ending points of a segment whose minimum is located at each index. The other part of the summation is easier to deal with; simply count how many times each element is the maximum of a pair. |
BIT seems very handy but I don't understand some parts of code in editorial. Can anyone explain how to use BIT to count Max and Min in Problem E? |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK