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

Автор AndreyPavlov, история, 22 месяца назад, По-русски

1780A - Hayato and School

Идея: AndreyPavlov
Подготовка: AndreyPavlov
Разбор: AndreyPavlov

Разбор
Решение (Python)
Решение (C++)

1780B - GCD Partition

Идея: 74TrAkToR
Подготовка: qualdoom
Разбор: qualdoom

Разбор
Решение (Python)
Решение (С++)

1780D - Bit Guessing Game

Идея: AndreyPavlov
Подготовка: qualdoom, AndreyPavlov
Разбор: qualdoom, AndreyPavlov

Разбор
Решение (Python)
Решение (С++)

1780E - Josuke and Complete Graph

Идея: IzhtskiyTimofey
Подготовка: IzhtskiyTimofey
Разбор: IzhtskiyTimofey

Разбор
Решение (Python)
Решение (С++)

1780F - Three Chairs

Идея: AndreyPavlov
Подготовка: AndreyPavlov
Разбор: qualdoom

Разбор
Решение (С++)

1780G - Delicious Dessert

Идея: IzhtskiyTimofey
Подготовка: IzhtskiyTimofey, AndreyPavlov
Разбор: IzhtskiyTimofey, AndreyPavlov

Решение
Решение (t4m0fey)
Решение (AndreyPavlov)
Разбор задач Codeforces Round 846 (Div. 2)
  • Проголосовать: нравится
  • +348
  • Проголосовать: не нравится

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

Ну и где решение C? Очень хочется узнать решение NP-сложной задачки :D

»
22 месяца назад, # |
  Проголосовать: нравится +46 Проголосовать: не нравится

Can't wait for the solution of C. Show us guys, how to solve NP-hard problems!

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

How come I see ACs still on C? No hacks were made on the problem?

»
22 месяца назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

Has no one made a hack for C yet?

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +37 Проголосовать: не нравится

    The tester code probably uses the same faulty algorithm so it would be to no avail.

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +57 Проголосовать: не нравится

    The author's solution is wrong. Therefore, any hack would still get AC, even if the output is incorrect.

»
22 месяца назад, # |
  Проголосовать: нравится +80 Проголосовать: не нравится

Problem D was very interesting :) ,one mistake and whole efforts of problemsetters and testers got ruined.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Weren't the incorrect greedy solutions for C O(n)? Why was the constraint on the problem O(n^2) then?

»
22 месяца назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

Weren't the incorrect greedy solutions for C O(n)? Why was the constraint on the problem O(n^2) then?

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    I think it was to allow solutions that did not use priority_queue or something similar

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand why problem C was unsolvable?

this is my code please give me a counterexample.

»
22 месяца назад, # |
  Проголосовать: нравится +19 Проголосовать: не нравится

On the last row of Tutorial of B:

"Let $$$s$$$ be the array $$$a$$$"

should probably be

"Let $$$s$$$ be the sum of the array $$$a$$$"

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    They probably used machine translation. The solution to E is impenetrable.

»
22 месяца назад, # |
Rev. 2   Проголосовать: нравится -21 Проголосовать: не нравится

delete

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +19 Проголосовать: не нравится

    It's a bad idea because some poor people like me spent ~30min trying to solve it and ~20min trying to understand why so many people solved such a hard problem xd

    • »
      »
      »
      22 месяца назад, # ^ |
      Rev. 2   Проголосовать: нравится -60 Проголосовать: не нравится

      delete

      • »
        »
        »
        »
        22 месяца назад, # ^ |
          Проголосовать: нравится +18 Проголосовать: не нравится

        No

      • »
        »
        »
        »
        22 месяца назад, # ^ |
          Проголосовать: нравится +33 Проголосовать: не нравится

        LOL. Some people solved C with incorrect solutions that they figured out within minutes and moved on. And those who realized that solution is incorrect will be punished. What kind of fairness is that?

»
22 месяца назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

problem E & F are so interesting. i enjoyed..

»
22 месяца назад, # |
  Проголосовать: нравится +95 Проголосовать: не нравится

My solution failed system tests on C because it got a better result than the author's one, lol

Spoiler
  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Did you found any counter-test for your solution?

  • »
    »
    22 месяца назад, # ^ |
    Rev. 2   Проголосовать: нравится +9 Проголосовать: не нравится

    They should have asked for the exact assignment instead of just the maximum value, so that they can check for a FAIL verdict correctly. (FYI, FAIL is the verdict when the submission found a better solution than the jury's) They might have found the issue before the contest if this was the case.

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

    lol

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

    how it should be 11 can u explain it bit more clearer...

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

      There are 6 guests who likes the dish #1, and the remaining 5 likes the #2. So if we split the 6 guests in table #1 and #2, and the 5 who like #2 at table #3, all 11 guests will be happy.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Problem G is quite similar to this problem. I had to modify my solution for the Educational round problem a bit to get AC on this round's G.

»
22 месяца назад, # |
Rev. 10   Проголосовать: нравится +22 Проголосовать: не нравится

A brief tutorial to problem E, for people like me who cannot follow the official one:

Value $$$x$$$ will appear in one of the edges iff: $$$L \leq px < (p+1)x \leq R$$$, where $$$p=ceil(\frac{L}{x})$$$.

Now we enumerate every value of $$$p$$$ and count the number of possible $$$x$$$ for each $$$p$$$.

For $$$p=p_0$$$, the above inequality gives range of x: $$$\frac{L}{p_0} \leq x \leq \frac{R}{p_0+1}$$$.

Note that there is a hidden constraint of $$$ceil(\frac{L}{x}) = p_0$$$. This translates to $$$\frac{L}{p_0} \leq x < \frac{L}{p_0-1}$$$.

Combine the two ranges above, we get, for $$$p=p_0$$$, possible values of $$$x$$$ satisfies: $$$\frac{L}{p_0} \leq x \leq min(\frac{R}{p_0+1}, floor(\frac{L-1}{p_0-1}))$$$.

And we use sqrt decomposition to solve this counting problem: For $$$p\leq sqrt(L)+1$$$, we compute the range of $$$x$$$. For $$$p > sqrt(L)+1$$$, we have $$$x \leq sqrt(L)$$$, so we can just iterate through all $$$x$$$ values.

Complexity is $$$O(sqrt(L))$$$.

»
22 месяца назад, # |
Rev. 7   Проголосовать: нравится +3 Проголосовать: не нравится

In C question greedy solution fail on test case-

1

11 3

1 1 1 1 1 1 2 2 2 2 2

5 4 2

correct answer for above testcase = 11, but greedy solution gives answer = 10

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain what was the intended solution for C, which turned out to be incorrect.

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +28 Проголосовать: не нравится

    Group people by what they like, and while there are groups of people and tables left, try to put the largest group on the largest table. If they don't fit, just use as many as you can and subtract that amount before deleting the table. If they do fit, delete the group of people and the table. "Answer" is however many people this process gives a table. The original might've been slightly different, but this is the silly wrong solution I got AC with.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In Problem D, in 2nd Tutorial (by qualdoom), for the condition (pt.2) where "$$$cnt-cnt_{new}\not=1$$$", can someone kindly explain the part, how this below equation is formed?

$$$m - k - 1 = cnt_{new} - cnt$$$

  • »
    »
    22 месяца назад, # ^ |
    Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

    Consider the real number is 24 its binary representation is 11000, and you need to guess this number. also, you can't subtract a number greater than the real number so if we subtracted 1 from real number which was 24, we will have 23 which its binary representation is 10111 so the number of unit bits has been changed and become 4-unit bits instead of 2 how this happened? hence, we now have 23 which has 4-unit bits and previous has 2 so the LSB at index (4-2+1) 0-based we add 1<<lsbIndex to obtain the real number. I hope that helped you.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Who can explain me, how to use mobius function in problem F?

»
22 месяца назад, # |
  Проголосовать: нравится +12 Проголосовать: не нравится

I don't know why. I want to comfort the author. qnq

»
22 месяца назад, # |
Rev. 4   Проголосовать: нравится +8 Проголосовать: не нравится

Can someone give a test case where this approach gives a wrong answer to C?

  1. Calculate how many meals of each type and how many tables of each size there are.

  2. Go through the meals, if there is a table with the same size as the count of that meal, then put those people to that table.

  3. Put remaining meal and table counts to two priority queues. While there are still some empty tables or not all people has a spot: take the top of meals and put them at the biggest empty table, if some people who like that meal are still left, check if there is empty table with that amount of spots, if there is put them there, otherwise put that number back to priority queue.

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

    Ok, here's a counterexample: tables are 2 2 3 and people are 1 1 1 1 2 2 2. The second step goes as no-op, so you will take ones into the third table (which has 3 seats) and then twos are placed in the remaining first/second table, which is not a correct strategy since you could have placed ones into first and second table and then remaining twos into the third table.

»
22 месяца назад, # |
Rev. 5   Проголосовать: нравится +3 Проголосовать: не нравится

My approach for D (similar to the second tutorial solution, it seems, but it doesn't explicitly need flags and might be easier to understand for some):

To determine whether the last bit is 0 or 1, we can simply subtract 1. If the last bit was a 1, then it becomes 0 and the $$$cnt$$$ decreases by exactly 1. Now that the last bit is already 0, we can check the second-to-last bit by subtracting 2. In general, if the last $$$k$$$ bits are set to 0 while the rest of the bits are unchanged from the original, we can test bit position $$$k$$$ (from the right) by subtracting $$$2^k$$$ and seeing if $$$cnt$$$ decreases by exactly 1.

But what happens when the bit at the position we're testing (let's say position $$$k$$$) is actually a 0? In that case, $$$cnt$$$ either stays the same or increases, so we know that this position originally has a 0. However, this attempted subtraction of $$$2^k$$$ ruins our desired pattern, because the bit at position $$$k$$$ changed from 0 to 1 and at least one bit on its left has changed. We would have wanted the bits from up to position $$$k$$$ to all be 0 and everything else unchanged in order to test the next bit, at position $$$k + 1$$$...

We can deal with this by "undoing" the latest undesirable subtraction by instead treating it as part of the next subtraction. Normally, the next subtraction is supposed to be $$$2^{k + 1}$$$, but since we already performed an undesirable subtraction of $$$2^k$$$, we simply subtract another $$$2^k$$$ to result in a overall subtraction of $$$2^{k + 1}$$$ as if the undesired subtraction never happened.

Note that there could be a sequence of undesirable subtractions, in which case, we just keep track of the total of the undesirable subtractions and treat them all as part of the next subtraction. Eventually, we would have to find a 1, which resolves all these undesirable subtractions. Once we find the MSB, the $$$cnt$$$ should become 0, at which point, we are done.

My submission: 190561881 ($$$acc$$$ is the accumulation of undesired subtractions that we are trying to undo)

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

Simple implementation for F 190544353

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

    Can you explain your solution if it's different from the editorial?

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

      I have similar solution. Mine is: calculate $$$dp[x]$$$ -- number of ways to choose a triple which value of gcd is $$$x$$$. To calculate amount of such triples for every $$$x$$$ sort an array then for every $$$x$$$ consider only numbers which are divisible by $$$x$$$, let their indices be $$$ind$$$, we should calculate sum over all $$$i < j$$$ of $$$ind_j - ind_i - 1$$$, we can calculate it by iterating over these indices storing sum of considered indices and their cnt. Then you have a $$$dp$$$ where you should substract from $$$dp[x]$$$ $$$dp[2 * x], dp[3 * x], ...$$$ to obtain correct answer.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
  1. What is the correct solution for problem C then, if it exists?
  2. In the editorial for problem E, it is mentioned that for g<L, ceil(L/g) will have O(sqrt(L)) values, how?
  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Consider for g<sqrt(L) and g>sqrt(L) respectively, for the first situation there's at most sqrt(L) values of g, so ceil(L/g) will have at most sqrt(L) values. For the second situation, we have 1<=ceil(L/g)<L/g+1<L/sqrt(L)+1=sqrt(L)+1, so there's at most ceil(sqrt(L)) different values.

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

      How to generate all values of ceil(L / i) where i belongs to [1, L] in O(sqrt(L))?

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

        First let i = 1 to ceil(sqrt(L))-1. For the remaining values, for m = L/ceil(sqrt(L)) to 1, calculate the range where ceil(L/i)=m.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I did the same thing in problem B yet kept getting TLE. Can someone point out my mistake. Thankyou!

while(test--)
    {
        int n;
        cin >> n;
        int * arr = new int[n];
        int * cuml = new int[n]; //Cuml is for cumalative sum
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            if(i==0)
            {
                cuml[i] = arr[i];
            }
            else
            {
                cuml[i] = arr[i] + cuml[i-1];
            }
        }
        int ans = 0;
        for(int i = 0; i < n-1; i++)
        {
            if(cuml[n-1]%cuml[i]==0)
            {
                ans = max(cuml[i],ans);
            }
            else
            {
                int temp = gcd(cuml[i],cuml[n-1]);
                ans = max(temp,ans);
                cerr << temp << '\n';
            }
            
        }        
        cout << ans << '\n';
    }

Please help !

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

What is the time complexity in F and why is it quick? IMHO we have O(N * 2^(an average number of prime divisors)) so it doesn't seem like a quick solution.

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

    lets think like this. you know the number of divisors of a number n is O(log(n)). now you are only iterating on SOME of these divisors.

  • »
    »
    22 месяца назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    Let $$$preres_x$$$ be the number of ways of choosing the triplets where the $$$gcd(a_i,b_j)$$$ is a multiple of $$$x$$$ and $$$res_x$$$ be the number of ways of choosing the triplets where $$$gcd(a_i,b_i)$$$ is exactly $$$x$$$. You can calculate array $$$res$$$ from $$$preres$$$. If you know the values of $$$res_y$$$ for all $$$y$$$ (where $$$y$$$ is a multiple of $$$x$$$) and $$$preres_x$$$, then you can calculate the value of $$$res_x$$$ and finally, $$$res_1$$$ will be the answer. You can do this by iterating from $$$3*10^5$$$ to $$$1$$$.

    The time complexity is sum of harmonic sequence $$$\sum_{x = 1}^{N} \left \lfloor \frac{N}{x} \right \rfloor$$$ which is equal to $$$O(nlogn)$$$.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Of all the rounds this had to happen in a round full of Jojo's references

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

Can someone please tell me what was wrong with C? My approach to solving C was: Group all the guests having the same liking. Now assign the biggest group to the largest table. If some guests are still left, insert them in the priority queue and continue this way.

How was C an NP hard problem?

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

    Your approach is incorrect. Consider the following scenario:

    • 6 people like dish 1, and 4 people like dish 2.
    • Three tables, with capacities 3, 3, and 4 respectively.

    If you assign the biggest group to the largest table, then the 4-table serves dish 1 (all four like it), one of the 3-tables serves dish 2 (all three like it), and the other 3-table serves dish 1 (two out of three like it), with one unsatisfied guest. But if you instead had both 3-tables serve dish 1 while the 4-table serves dish 2, then everyone would be satisfied.

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

Even though C couldn't be solved with given constraints during the contest I think it's better to lower constraints to make it solvable, write a proper solution and post the updated solution in the editorial, instead of completely removing it from the contest and archive. The problem is not bad at all, tbh.

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

    It's NP-hard, which means it almost certainly cannot be solved in polynomial time. Constraints would require $$$n$$$ to be about $$$20$$$, or maybe even less! The brute-force solution would be incredibly dull. There may be heuristics to speed it up and allow a little higher constraints, but this is not suitable for CP.

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

      Doesn't mean it can't be solved at all. Better to keep a modified version with extremely low constraints than to remove it completely.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anybody tell me "correct" solution of C? or at least some ideas

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anybody tell me "correct" solution of C? or at least some ideas

»
22 месяца назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

I would surely become a specialist after this round (rank 16) but...... It's unrated! And problem G is kind of rigid that almost everyone who has learnt sam before can solve it.

But anyway,the problems are excellent!

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

can someone explain what's wrong with problem C. Instead of posting correct solution why authors completely removed problem C

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

    There is no correct solution to that problem. It is a NP-hard problem which cannot be solved in polynomial time

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

D was quite confusing cuz I have never solve interactive problems lol. F was easier than normal :V.

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand the tutorial for problem G. Can anyone explain the idea of it?

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Does any one know how to calculate the time complexity of G? More detailedly,the time complexity of find the number of x,which is a factor of n, that l<=x<=r? The solution implied that is almost nlog(n) in total , but I can't understand why.

»
18 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

how do we get the root L bound in problem E. Can someone prove

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

Hi Sorry if my doubt is too silly

In this question, 1780B — GCD Partition. For the given testcase:

3

1 4 5

what is the value of k for this due to which maximum score of 5 is coming? like are we partioning this array?