Блог пользователя triple__a

Автор triple__a, история, 6 лет назад, По-английски

Hope you enjoy those tasks!

1332A - Exercising Walk

Tutorial
Solution (python by pikmike)
Solution (C++)

1332B - Composite Coloring

Tutorial
Solution (python by pikmike)
Solution (C++)

1332C - K-Complete Word

Tutorial
Solution(python by pikmike)
Solution(C++)

1332D - Walk on Matrix

Tutorial
Solution(python by pikmike)

1332E - Height All the Same

Tutorial
Solution 1(python by pikmike)
Solution 2(python by pikmike)

1332F - Independent Set

Tutorial
Solution(C++)

1332G - No Monotone Triples

Contributor of idea of the solution: awoo

Tutorial
Solution (C++)
Разбор задач Codeforces Round 630 (Div. 2)
  • Проголосовать: нравится
  • +279
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

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

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +32 Проголосовать: не нравится

Video tutorial for D

Here you can see how to prove the described solution, which is similar with the editorial's approach.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for such a fast editorial! Nice round!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Fastest editorial in the west :3

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +10 Проголосовать: не нравится

There is the tutorial of problem 1327A instead of 1332A
UPD: now fixed

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Why editorial for A is an editorial to another problem?

EDIT: fixed now

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
6 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится -39 Проголосовать: не нравится

.

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +33 Проголосовать: не нравится

Why so many people get "Wrong answer on test 57"(problem E)?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

People have used DSU/DFS in their solutions for problem C. Could someone explain how that works?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +2 Проголосовать: не нравится

    I approached C using DSU. For this, we needed to find out about what all indices should have the same char. Now, let's consider a string of length n and k=k. Condition 1: A/c to question indices i, i+k, i+2k.... should have the same char at their index. Hence, we will union(i,i+k) , union(i, i+2k) ..... Condition 2: String should be a palindrome. So, index (0,n-1) , (1,n-2) , (2,n-3)...should have the same char at these positions. Hence, we will also do a union of these indices. After these operations, our DSU will provide which all indices belong to the same connected component. Now, that we have the info about the connected components we can easily find the char which has occurred the most number of times in that component. The rest of the char needs to be replaced for that component and added to our overall sum.

    Sorry for my bad English.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    For dfs, if you are currently at position $$$i$$$, the position $$$i+k$$$ and $$$n-1-i$$$ should have the same character as position $$$i$$$. So you can do dfs an find which positions should have same characters and calculate the answer based on which character appears the most times on each group. Similar idea to DSU solution.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +28 Проголосовать: не нравится

The round definitely made me think (and question my existence). Kudos to triple__a.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

I am not satisfied with the pretests of C. My $$$O(n^2)$$$ solution passed them. It would have been trivial to fix it, so now I am feeling a bit bitter.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

nice 1;

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

C is doable with dfs. It'll be an easier approach.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -6 Проголосовать: не нравится

I want to know if the input is 1 12 6 3 5 7 11 13 17 19 23 29 31 37 The output is 12 1 2 3 4 5 6 7 8 9 10 11 12 but I think the first data could belong to the color 2 so we can use only 11 color

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Tiny correction triple__a, You've written Solution(C++) but the code isn't C++ for problem C. Thanks for the fast editorial, and a great round

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +90 Проголосовать: не нравится

I would like to give an alternative solution for E using dp and matrices. From the editorial, we have a proof that the answer only depends on the number of ways choosing odd number of odd cells and that of choosing even number of odd cells.

Let $$$dp_{odd}[i]$$$ and $$$dp_{even}[i]$$$ be the number of ways choosing $$$i$$$ cells with odd number of odd cells and even number of odd cells respectively.

$$$dp_{odd}[i] = dp_{odd}[i-1] \cdot E + dp_{even}[i-1] \cdot O$$$

$$$dp_{even}[i] = dp_{even}[i-1] \cdot E + dp_{odd}[i-1] \cdot O$$$

Then we can use matrix and binary exponentiation to calculate the answer.

74978938

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is there solution for D if we say that values in matrices are a[i][j]<=1e5?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +24 Проголосовать: не нравится

triple__a Thanks for such a good round in these days!! No queueforces even after Contest and fast editorial!! Kudos

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I have done c using dsu it is efficient in this manner

but anyone did it using dp or is it not possible ? explain your solution if possible(any complexity)?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

My solution of E failed on test 57. I removed b % phi(mod) in the power function and it passed. Can anyone tell why? (a ^ b) % m = (a ^ (b % phi(m) ) % m. Since m is prime, so phi(m) = m-1 Although it was not needed, I think this shouldn't fail.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

problem C first solution — wrong language (python code, linked as c++)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

1 13 6 10 15 7 11 13 17 19 23 29 31 37 41 for this input m = 11 but my code give m = 12 and still accepted.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I think $$$k=3$$$ case is harder for problem F, I understand how to check if answer is 3 or 0, but how to reproduce the indices for $$$k=3$$$?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

How to write spj for D?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For E, can someone give more details about how to apply Newton expansion? I really cannot see a direct mapping :'(.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

for 1332A can someone please explain why shouldnt we consider both height and width at the same time

why taking only x axis in calculations. thank u!

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I solved F in this way.dp[0][x] and dp[1][x] mean the results in x's subtree and x in edge induced subgraphs.than the subtree combine with the father and add to the father node so I get this:

dp[0][x]=dp[0][x]+dp[0][x]*(dp[0][ed[i].v]+dp[1][ed[i].v]);
    dp[1][x]=dp[1][x]+dp[1][x]*dp[0][ed[i].v];

initially, every node's dp[0][x]=1; dp[1][x]=1; for example,1-2,2-3,2-4, first 3 and 4 node are both (1,1), than in node 2, first I choose the 3 to mix 2 ,node 2 get (3,2) ,means now 2-3 this tree's ,with node 2 in edge induced subgraphs, results .than mix 4 to 2 ,get node 2 (9,4) as the question required, add all dp[0][x] and dp[1][x] to answer and remove the one node set , so ans-=2*n it shows right in two examples. what's wrong with me? could someone help me?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Writing several times within one explanation that it is easy and how easy it is does not makes it more understandable, or even easy.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

hope everyone uploads the tutorials this fast as we r eager and curious about problems solutions!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +43 Проголосовать: не нравится

How did my solution for problem G get TLE on test 109?

The pre-calculation costs only 202ms on test 109, and answering a query only needs constant time. So I don't think it could get TLE. However, the same code got accepted with 64-bit compiler (Submission). So strange.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Python solution for Problem C is incorrectly mentioned as C++, please correct it triple__a.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I was pretty sure that there is a formula solution for E, but during the contest I wasn't able to come up with a correct idea... So now you can check out my solution that uses matrix multiplication:74968680

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

Why verdict on task D test 2 is WA? Difference equal to 1...

1
Output
3 3
3 1 0
2 3 0
2 2 1
Answer
3 4
7 3 3 1
4 8 3 6
7 7 7 3
Comment
wrong answer difference is 0 - 0 = 0
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I used DSU for C. My submission

We can start by assigning each position of the string as a disjoint set.

Then we do a union on all those positions which have to be equal according the the Palindrome condition and the Period of size K condition.

After that I assigned characters to each disjoint set greedily. (ie for every disjoint set of positions, I assigned the character with maximum occurence in the particular disjoint set).

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится -7 Проголосовать: не нравится

To begin, this post has nothing to do with the contest (i just need help with a topic is all)..

I want to learn "binary search the answer" and other details of the algorithm, I tried searching the internet and Codeforces blogs but couldn't find anything specific to the topic that I was looking for.. I think I understand discrete binary search (about monotonicity of the sequence for some predicate.. but I think that's standard and commonplace) but I need to master the details so that I can solve problems that aren't "apparent to involve anything with binary search yet does so beautifully". Can anyone help?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone pls explain C in ann easy way?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Problem

    Given a string s of length n and a "period" k, find the minimal number of changes such that the string becomes a palindrome AND every kth character is identical (i.e. s[i] = s[i+k] = s[i+2*k] = ..)

    For eg. if the string is "abba", and k=2, this is not a valid string because the 0th and 2nd chars are different, and so are the 1st and 3rd. So you'd need to change 2 characters to make this "aaaa" and so the answer would be 2.

    Solution

    • For any position in the string, i, every i+k'th character needs to be identical. Also since its a palindrome, the mirror character for each of these characters (i.e. mirrors of i, i+k, etc.) need to be identical as well
    • Now find the character that's used most frequently, and change the remaining characters to that
    • For eg., consider "abcbaa" and k = 3. In the first iteration (i.e. i=0), you'd find the maximum occurring characters among a[0] (the ith character), a[0+3] (the i+kth character), a[6-1-0] (mirror of the ith char), a[6-1-3] (mirror of the i+kth char) i.e. a[0], a[3], a[5], a[2] which are "a", "b", "a" and "c" respectively. Clearly "a" is most frequent, so change all remaining (4-2 = 2) characters to "a" making the overall string "abaaaa"
    • Repeat this for all i where i<k
    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      How are we ensuring putting the most frequent character will make the String a palindrome?

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
        Rev. 2  
        Проголосовать: нравится 0 Проголосовать: не нравится

        For every character in position i, you're collecting all i+kth chars, and also finding the mirror positioned chars for each of them. Then you look for the most frequent amongst all of these and change all 4 to this character.

        So if the string is "abcaab" and k = 3:

        1) In your first pass (i.e., i=0) you will consider 4 characters: a b c a a b. Since a is the most frequent character, change all 4 to a b a a a a. It's still not a palindrome at this stage.

        2) In your next pass (i=1), you will consider the following: a b a a a a (this is your entire set of i, i+k palin_i, palin_i+k). Since "b" and "a" are equally frequent, you could use either one (say you choose "b") and your string becomes a b a a b a, which is now a palindrome.

        3) The next and final iteration (i=2) would consider the same four elements as the first iteration. All 4 are already the same and nothing else needs to be done.

        At the end of this, you have a string that meets the criteria, including being a palindrome.

        • »
          »
          »
          »
          »
          6 лет назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          I saw your solution for this problem I must it's very easy to understand just one query why are we outputing ans/2 in the end instead of ans

          Thank you

          • »
            »
            »
            »
            »
            »
            6 лет назад, скрыть # ^ |
             
            Проголосовать: нравится 0 Проголосовать: не нравится

            It's because each set of points is visited twice. So when the string is "abcaab", and k = 3, then at the first iteration (i=0) I visit 4 indices 0, 3 (0 + k), 5 (mirror of 0), 2 (mirror of 0 + k). On the second iteration (i=1), I visit 1 and 4 — twice (since 1 and 4 are the mirror positions of themselves). Then in the final iteration, I visit 4 indices — 2, 5, 3, 0 (the same as the first iteration). All for a total of 4 + 4 + 4 = 12 times.

            So n=6 and I've counted a total of 12, resulting in a factor of 2. I could avoid this division by 2 by not overcounting in the first place. For eg. I could stop iterating at k = n/2, and in the last index, make sure that if the mirror positions are the same as the indices, not to count them twice. This involves a couple of extra conditions, and sometimes its just easier to overcount and account for it at the end, so I chose that route instead.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -18 Проголосовать: не нравится

In problem C, n is supposed to be divisible by k. However, for test 3, n=200000 and k=64. Isn't that wrong?

»
6 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please tell me where my logic is wrong for problem F? https://ideone.com/zhB5cl

It's failing for the sample test cases too.

EDIT: Was able to solve it. https://mirror.codeforces.com/contest/1332/submission/75045994

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Noob here so sorry if this is obvious

In question A, why don't we add an or condition for the y-axis also? (x2<=x-a+b<=x1 || y2<= y-c+d <= y1) I mean, what there's a case if x condition is satisfied but not y

Thanks

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +4 Проголосовать: не нравится

    You are correct, both the x and y-axis have to be considered. The author simply skipped the y-axis since he said at the beginning that it was the same for both.

    You have to check both x1 <= x+b-a <= x2 and y1 <= y+d-x <= y2, plus the special cases also mentioned in the tutorial.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +26 Проголосовать: не нравится

It's so sad that my E FSTed.. I can reach rk1 if it's not.. Again a quick power error!!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

В Е есть ещё несложное решение, где не надо думать. Заметим, что если мы знаем кол-во полосок с чётной и нечётной суммой $$$1*k$$$ и $$$1*t$$$, мы можем узнать кол-во полосок с каждой суммой для $$$1*(k+t)$$$. А дальше то же самое с $$$n*k$$$ и $$$n*t$$$ -> $$$n*(k+t)$$$. И узнаем ответ для $$$n$$$ и $$$m$$$, как в быстром возведении в степень. Работает за $$$\log(n)+\log(m)$$$.

Код
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

I liked E among the first 5 problems of the round.

I couldn't get accept during the round but fixed the problem after the contest.

the approach of solving this problem was a bit cooler than the others !

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

First c++ code is python code in problem C

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone elaborate why we need *(MOD+1) in the solution1 of problem E? Thanks in advance.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

can anyone please explain how, dp is working in the f question how they ended up with these three equations??

Thank you :)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone explain proof why there can’t be more than 11 in B ? it’s little foggy in editorial

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    First remember that any given $$$a$$$ is a composite number, that is it can be expressed as a product of $$$2$$$ integers both strictly greater than $$$1$$$.

    $$$ $$$

    Let $$$a$$$ be one such integer, $$$a \leq 1000$$$. Let $$$a = bc$$$ with $$$b,c \gt 1$$$. If both $$$b$$$ and $$$c$$$ are strictly greater than $$$\sqrt{1000}$$$ then $$$a \gt 1000$$$ which yields a contradiction.

    $$$ $$$

    Therefore at least one of them is smaller than $$$\sqrt{1000}$$$, and from now on let's consider that $$$b\leq\sqrt{1000}$$$. If $$$b$$$ is not prime, then there is one prime (smaller than $$$b$$$, therefore smaller than $$$\sqrt{1000}$$$) which divides $$$b$$$ and therefore divides $$$a$$$. If $$$b$$$ is prime, then $$$b$$$ is a prime number smaller than $$$\sqrt{1000}$$$ that divides $$$a$$$.

    $$$ $$$

    Now we know that for any given $$$a$$$, there is always a prime number smaller than $$$\sqrt{1000}$$$ which divides it. And it turns out there are only $$$11$$$ primes that meet this requirement !

    $$$ $$$

    So any given $$$a$$$ will be divisible by one of those $$$11$$$ primes. We can then greedily assign our colors with this in mind :)

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In $$$E$$$, the omitted proof for matching grids with an even $$$k$$$ could be the following :

$$$ $$$

Take away all grids for which $$$a_{0,0} = k$$$ and do a regular matching. Then the set of all grids such that $$$a_{0, 0} = k$$$ remains unmatched. What we should do with it is applying the same trick but with an other $$$a_{i,j}$$$. The remaining unmatched grids will then be the ones for which $$$a_{0,0}=a_{i_,j}=k$$$. We may keep doing this until only the grid with $$$a_{i,j}=k$$$ for all $$$(i,j)$$$ remains. This is the only unmatched grid, and it is a valid one, hence the formula.

$$$ $$$

Thank you OP and every organizers for the contest ;)

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I have used the exact same method as stated in the editorial for Question B — Composite Coloring but I got one of the test cases (not pretests) wrong. Here's my submission 74980482. Can anyone tell me why my code was wrong? Thanks.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How solve C using dfs. I saw some code used dfs to solve. But no node comes in my brain...

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Basically for each i in range 1 <= i <= n — k , s[i] = s[n + 1 — i] , and s[i] = s[i + k]. Now you have to count how many letters should be the same in this cycle. The nodes are the indices. You can see my code, maybe you will understand.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +20 Проголосовать: не нравится

Can anyone explain how the dp formula in problem F works in detail? I don't understand it. Thanks a lot.

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Great contest and editorial

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Great contest :))

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

What's the meaning of

s1=lower_bound(st1+1,st1+p1+1,i,cmp1)-st1-1, s2=lower_bound(st2+1,st2+p2+1,i,cmp2)-st2-1;

in the code of problem G?

Why can't we use s1=p1,s2=p2; instead ?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    because you need to make the answer fit into range [L,R] instead of [L,n].

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Since the code is doing precalculating, where does this $$$[L,R]$$$ come from?

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        since you should do carefully when $$$R=c[L]$$$. (check the code for notation.)

        • »
          »
          »
          »
          »
          6 лет назад, скрыть # ^ |
          Rev. 2  
          Проголосовать: нравится 0 Проголосовать: не нравится

          Now I think you are coping with equal elements.

          s1:右边最后一个大于等于a[i]的位置+1 -> 右边第一个小于a[i]的位置

          s2:右边最后一个小于等于a[i]的位置+1 -> 右边第一个大于a[i]的位置

          So you will skip the equal elements to find "the closest to the right element greater than the top one" and "the closest to the right element smaller than the top one".

          BTW:The code is slightly different from the solution about details. Hope you could provide one that does what the solution says.

          • »
            »
            »
            »
            »
            »
            6 лет назад, скрыть # ^ |
            Rev. 2  
            Проголосовать: нравится 0 Проголосовать: не нравится

            the s1 and s2 is coping with the case where elements is the same as you mentioned. However, for range $$$L$$$ to $$$c[L]$$$, we cannot simply pick the element in the stack different from $$$a[i]$$$.

            Consider the following case:

            5 4 6 3 7 6
            

            in this case and $$$i=1$$$, you are supposed to pick 3 and 7 instead of the second top element of the stack which should be 4 and 6 if you follow the implementation the code presents. (the conception is mentioned in the $$$O(n^2)$$$ indeed)

            • »
              »
              »
              »
              »
              »
              »
              6 лет назад, скрыть # ^ |
               
              Проголосовать: нравится 0 Проголосовать: не нравится

              Can you explain more how to recover the min/max after we got the first and last index in a k=4 sequence? I am not understanding how to do this with just the stack.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Nice

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I wonder if there is a typo in the formula of the combinatorics solution for task E. It should be $$$(E+O)^{nm}=\sum_{i=0}^{nm/2}E^{2i}O^{nm-2i}\binom{nm}{2i} + \sum_{i=1}^{nm/2}E^{2i-1}O^{nm-(2i-1)}\binom{nm}{2i-1}$$$ instead of $$$(E+O)^{nm}=\sum_{i=0}^{nm/2}E^{2i}O^{2i}\binom{nm}{2i} + \sum_{i=1}^{nm/2}E^{2i-1}O^{2i-1}\binom{nm}{2i-1}$$$ And similar for $$$(E-O)^{nm}$$$, could someone explain it?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -31 Проголосовать: не нравится

Worst contest ever

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

It's a very enjoyable round

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can someone who got acc on D to tell me how they thought to get to that solution?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Just a doubt for D though, what if we get a problem stating that find the maximum value you can get for going through (1,1) to (n,m), this dp in question is wrong for sure, then what is the correct dp or approach to solve this type of problem

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

I know its trivial but,
Can you please tell me the intuition behind Problem B?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Because $$$a_i\leq 1000$$$, while in other problems $$$a_i$$$ is always $$$\leq 10^9$$$. It's very strange. And that means maybe we can find the answer from the relationship between $$$1000$$$ and $$$11$$$. Then it may be easier to realize that there are only 11 primes smaller than $$$\sqrt{1000}$$$.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem A, why I need the condition a+b=0?

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In 1332A - Exercising Walk why can't we set y = y-(d-c). If I set x = x-(a-b) and y = y-(d-c), my output is wrong.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is B solvable using graph coloring where edge exist between two elements if their gcd is 1.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +7 Проголосовать: не нравится

I'm just curious in problem d ..since that dp algorithm fails...could there be a different dp approach which would actually lead to a correct solution triple__a ?

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +18 Проголосовать: не нравится

I think it's proper for a Chinese video solution(no it's just a recording) appearing under a Chinese contest editorial.

So, the link here : bilibili BV1at4y1m7V8.

Bullet screens and comments are welcomed.

If you like the video, please subscribe and leave a like (and 一键三连)!

If you don't like the video, leave your suggestions in the comment area below!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can anyone explain Problem D. Tutorial isn't enough.

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +67 Проголосовать: не нравится

Another take on problem E:

Define polynomial multiplication like this: $$$x^{i} \times x^{j} = x^{(i + j) \mod 2}$$$. For example, $$$(1 + x)^{2} = 1 + 2x + x^{2}$$$ will be simply $$$2 + 2x$$$.

Let $$$p = $$$ number of even numbers in range $$$[L, R]$$$ and $$$q = $$$ number of odd numbers in range $$$[L, R]$$$. For even $$$nm$$$, the answer is the constant term of $$$(p + qx)^{nm}$$$, and for odd $$$nm$$$ the answer is sum of its both coefficients. Complexity will be $$$O(\log nm)$$$ with binary exponentiation. (Code)

This solution is also easy to generalize for different modulo classes and the polynomial multiplication be made faster with FFT if the modulo class is big.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

In Problem E, It's mentioned that There is a general solution for this kind of problems.Can anyone provide any resource and more problems like that?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Let me appreciate the fact, Problem E was really nice!!

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Please Explain C without DSU

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why we have done ans/2 in 1332-C ?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I'm trying to understand the solution of 1332E - Height All the Same. In the proof of observation 2 it says that you connect 2 cells of the same parity via a route. Let's take a more simple problem say you have an empty grid and you have 2 * k marked cells in this grid. You want to connect these up into k pairs such that the routes connecting different pairs are not crossing(i.e. they don't have a common cell). I think the proof of observation 2 assumes this is always possible. I don't yet see it why this is true.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I realized that R is only a bound in the beginning. I misunderstood the problem statement. Although the solution also stands when R is a bound in the end because when you have 2 * k marked cells on an n * m grid you can connect them such that no 2 routes cross each other. Just take a Hamiltonian path in the grid(not too hard to find one) and pair up the elements along this path. What this means is that you can change the parity of the even number of elements by raising their value by 1 and raising the value of the elements along the routes by 2. This obviously requires the routes not to go through cells which already have height R.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    "You want to connect these up into k pairs such that the routes connecting different pairs are not crossing(i.e. they don't have a common cell)"

    crossing is allowed.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Yeah I figured that out in the meantime. I meant that if R is a bound in the beginning and the end as well and you have n * m % 2 == 1, L = 0 and R = 2 and an even number of 1s and an odd number of 0s you have to be able to connect them pairwise without crossing. This is possible, just line up the pairs on one of the Hamiltonian paths of the grid.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What will be the correct DP algorithm for the D problem I want tabulation method correct algorithm for finding the path for a given grid where the and of all the element on the grid is maximum as possible

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

can some one explain the easy intuition solution of pikmike (problem E) ?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    You can just draw it out, like for these grids:

    1x2, l = 1, r = 2;

    1 1

    1 2

    2 1

    2 2

    1x2, l = 1, r = 3

    1 1

    1 2

    1 3

    2 1

    2 2

    2 3

    3 1

    3 2

    3 3

    There's 4 grids for the first one and half of them are even, and there's 9 grids for the second one and (9+1)/2 of them are even. I assumed it would be like that for all grids.

    The intuitive part is that imagining all combinations of numbers, something like half of them will be even (we're looking for evens because there needs to be an even amount of odds, so sum has to be even).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Guys I found D solution so non-trivial and brilliant. How did you guys figured this out during the contest. What is the general thought process for approaching constructive algorithm based question.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

triple__a I am wondering what is the checker code for problem D :)

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem F, don't we need to consider stack overflow when using recursive dfs since n <= 3e5?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't have many ideas when I meet the constructive algorithm problems. Such as 630 div.2 D. I found that advanced CF players can solve those problems quickly. Can anyone give me some suggestions to improve my skill? Thanks a lot.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

An alternative solution for D. Consider the example —

for k = 9 or 1001, consider the solution -
[11111 1111     0]
[10000 11111 1001]

The algorithm uses: 11111 -> 10000 -> 11111 -> 1001, answer = 0
Optimal answer: 11111 -> 1111 -> 11111 -> 1001, answer = k

int k; cin >> k;
int d = (int)log2(k) + 1;
vvi ans =   {   
                {(1 << (d + 1)) - 1,        (1 << d) - 1,           0}, 
                {1 << d,                    (1 << (d + 1)) - 1,     k}  
            };
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I used a quite different approach to solve the dp in problem F. Here instead of removing edges I just calculated the value of w(H) for any subgraph that may lie in the subtree of v. Then to calculate this assume dp[v][0] be the total value of sum(w(H)) for any subgraph that does not have any edge coming out of v, dp[v][1] be the value when there is at least one edge from the vertex v to at least one of its children but v is not counted in those independent subsets, and finally dp[v][2] be the value such that there is at least one edge to a child and also v is counted in the subsets. Then we obtain the following dp:

//for all to belong to the children of v;
dp[v][0]=Product(dp[to][0]+dp[to][1]+dp[to][2])
dp[v][1]=Product(3*dp[to][0]+2*dp[to][1]+2*dp[to][1])-dp[v][0]
dp[v][2]=Product(2*dp[to][0]+2*dp[to][1]+dp[to][2])-dp[v][0]

We can find it by assuming that if there are no edges from v then we can add any subgraph in a child to another subgraph in other children including a null graph. And by multiplying them we sort of add the independent sets together. Similarly, for the case where there is at least one edge from v to a child, we consider both cases for each child of whether to include it or not and subtract the cases when we don't include any one of them (inclusion-exclusion). The link to my submission: 76447494.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand one thing in solution of B. In the first test i have 10 different numbers, but also the number 11.

23 437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961

11 8 2 3 1 2 7 1 2 2 3 7 3 4 4 5 5 6 6 7 7 8 10 11

There is not the number 9. Why it isn't working? Please help

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

is there a problem in my code , i am getting tle on test 5 https://mirror.codeforces.com/contest/1332/submission/83755735

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

triple__a I guess solution provided for 1332A contains some bugs. kindly verify it by checking against following test case

1 2 3 0 0 0 0 0 0 1 0

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi awoo, can you explain this line for your solution for Div2D?

However, we should notice that we can only discard the suboptimal result if and only if anssuboptimal & ansoptimal = anssuboptimal rather than anssuboptimal < ansoptimal.
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

how the question 1332C — K-Complete Word is from topics dsu and dfs, it didn't use any such algorithm but is listed in topic tags of dfs and dsu.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

whu i get wrong answer in test 1 can someone help me??problem (B) https://ideone.com/uKkSIq

»
16 месяцев назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

Explanation of the Dynamic Programming Solution for Problem C

The goal is to construct a palindrome of length ( K ) such that every block of size ( K ) in the string matches this palindrome. Here's how we approach it:

1. Palindrome Constraints

For every character in a block of size ( K ) at index ( i ) (( i < \frac{K+1}{2} )), there are 26 possible characters (all lowercase Latin letters). To form a palindrome, the character at index ( i ) must equal the character at index ( K-i-1 ).

2. Recursive Transition

We recursively process each index ( i ) from ( 0 ) to ( \frac{K+1}{2} — 1 ), fixing one of the 26 options for characters at indices ( i ) and ( K-i-1 ). At each step, we calculate the cost of making this choice and add it to the result of the recursive call for ( i+1 ).

3. Cost Calculation

For each character ( ch ) at index ( i ), the cost is computed as: [ \text{cost} = 2 \times \text{blocks} — \text{map}[i][ch] — \text{map}[K-i-1][ch] ] - ( \text{blocks} = \frac{N}{K} ), which is the number of ( K )-sized blocks in the string. - ( \text{map}[i][ch] ) counts how many times the character ( ch ) appears at index ( i ) across all blocks. - ( \text{map}[K-i-1][ch] ) similarly counts occurrences at index ( K-i-1 ).

If ( K ) is odd and ( i = \frac{K}{2} ), only the character at ( i ) needs to be considered, so the cost is halved.

4. Base Case

When ( i \geq \frac{K+1}{2} ), the recursion terminates since all relevant indices have been processed, and no additional cost is needed.

5. Dynamic Programming (Memoization)

The DP array ( dp[i] ) stores the minimum cost for indices from ( i ) onward to avoid recomputation. If ( dp[i] ) has been computed (( dp[i] \neq -1 )), we return its value instead of recalculating.

6. Implementation Details

  • We first populate a frequency map (map) that counts character occurrences at each index in all blocks.
  • We then use the recursive function f(i, N, K, map, dp) to calculate the minimum cost starting from index ( i ), leveraging memoization for efficiency.

Code:

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner ab = new Scanner(System.in);
        int T = ab.nextInt(); // Number of test cases
        while (T-- > 0) {
            int N = ab.nextInt(); // Length of the string
            int K = ab.nextInt(); // Size of each block
            String S = ab.next();

            // Frequency map: counts occurrences of each character for each index in a block
            int[][] map = new int[K][26];
            for (int i = 0; i < N; i++) {
                int ind = i % K; // Position in the block
                int ch = S.charAt(i) - 'a'; // Character index
                ++map[ind][ch];
            }

            // DP array for memoization
            int[] dp = new int[K / 2 + 1];
            Arrays.fill(dp, -1);

            // Compute and print the result for this test case
            System.out.println(f(0, N, K, map, dp));
        }
    }

    // Recursive function to calculate the minimum cost
    static int f(int i, int N, int K, int[][] map, int[] dp) {
        if (i >= (K + 1) / 2) return 0; // Base case
        if (dp[i] != -1) return dp[i]; // Return memoized result

        int ans = N; // Initialize with a large value
        int blocks = N / K;

        // Try all 26 characters for index i
        for (int ch = 0; ch < 26; ch++) {
            int cost = 2 * blocks - map[i][ch] - map[K - i - 1][ch];
            if (K % 2 != 0 && i == K / 2) cost /= 2; // Special case for the middle index if K is odd
            ans = Math.min(ans, cost + f(i + 1, N, K, map, dp));
        }

        return dp[i] = ans; // Store and return the result
    }
}
»
8 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Sorry for replying to an old post.