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

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

1475A - Odd Divisor

Problem authors: Stepavly, Supermagzzz

Editorial
Solution

1475B - New Year's Number

Problem author: MikeMirzayanov

Editorial
Solution

1475C - Ball in Berland

Problem authors: Stepavly, Supermagzzz

Editorial
Solution

1475D - Cleaning the Phone

Problem authors: Stepavly, Supermagzzz

Editorial
Solution

1475E - Advertising Agency

Problem authors: Stepavly, Supermagzzz

Editorial
Solution

1475F - Unusual Matrix

Problem authors: Stepavly, Supermagzzz

Editorial
Solution

1475G - Strange Beauty

Problem author: MikeMirzayanov

Editorial
Solution
Разбор задач Codeforces Round 697 (Div. 3)
  • Проголосовать: нравится
  • +127
  • Проголосовать: не нравится

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

F was almost a duplicate of USACO's 'Left Out': http://www.usaco.org/index.php?page=viewproblem2&cpid=942 Really like the idea regardless

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

    also it makes no sense to apply the same operation more than once (by the property of the xor operation).

    Can someone explain this line in the F editorial? I understand that applying the operation twice on the same row/column would again switch it back to the previous, but how applying the operation to a row, then to a column, and then again to the same row doesn't make sense?

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

    in E , i am having trouble while taking mod

    when i submitted without taking mod while calculating factorial then i got overflow. 106346070

    but when i changed and gave mod while calculaating factorial itself then , i got the answer as 0 106343305

    pls someone help me out.

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

Thanks for the contest! I recorded my participation and narrated over it -- https://www.youtube.com/watch?v=0eZsX0BWtyY (video is done processing + I've added chapter timestamps).

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

Interesting problemset, not too tough nor too easy. It was definitely more on an easier end, but considering it was a Div3 I enjoyed it.

I especially liked the appearance of many different parts of CP and different topics, not just ad-hoc problems, e. g. binary search, dp, multiplicative inverse/binary exponentation, math, observation...

I think beginners can greatly benefit from this experience regardless of the long queue. Thanks to the problemsetters!

UPD: For those wondering where binary search appeared — apparently I overcomplicated the fourth problem and solved using binary search, Might have not been the best idea, but made it a nice problem for me. :P

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

What will be the complexity of G. Strange Beauty??

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

i solved D using dp , never realized it could be done using binary search

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

What will be the time complexity of last question??

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

My own set of video solutions, probably with some overcomplications (as usual...)

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

Check out my explanations of problem F and G — solution

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

Screencast with narration + Solution Explanations: https://youtu.be/WynaHclse_4

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

How to solve D if conveniences b[i]<=1e5 ?

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

My D is giving runtime error on 2nd case. exit code -12343838 something like this. I tried it for many test cases but it is giving me right output as well as i tried it for some cases which are shown for 2nd test-case also. It is working fine. Can anyone help me? submission: 105393105

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

Anyone want to increase their hack count??

Here you go.... 105318690

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

For problem D, shouldn't 0-1 knapsack work? I did have a feeling that the convenience values of only 1 & 2 were important, but my initial intuition was to simply calculate the maximum convenience for memory up to (total memory of all applications — m). Then, the minimum convenience lost should be (total convenience of all applications — maximum convenience possible). My solution doesn't seem to work, but I am not sure why this solution would not work for all cases.

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

Why can't we consider a number (say n) as it's own divisor in problem A? I mean a number is surely its own divisor! I don't think the problem setters have accounted for this, or am I missing something?

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

In D why this does not works? Sort the array consistimg of all a[i] ( imp nd regualr both) in inc. order.

Select from beginning apps till the sum does not exceeds m. Then try to replace selected important apps by remaining regular apps.

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

Can anybody help me out with the difference between the 2 codes which are almost the same except for the fact in 1st I am directly using vector size and in the second submission I am saving the size in tmp variable and then using it: 105405168 [WA ON TC 7] 105408860 [AC]

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

    size() returns an unsigned integer. It can give unusual output in case when size is 0 or in similar cases. So it is better to typecast it. When you stored the value in temp what you did was basically typecasted it to int and it worked fine.

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

As many people are posting solutions on their youtube channel. I would love to see 15 minutes video from top performers like tourist. Hopefully he will add some contest video to his channel soon.

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

(Regarding Problem C)

Why does this TLE?

Also, in other sources for the same problem where I use a C-like array instead of std::vector (though this would also apply for std::vector, I think), why is it that occ[2][MAX_NR + 1] is considerably faster than occ[MAX_NR + 1][2] (514 ms. vs. 1357 ms.)? I kind of read about the difference between those two some time ago, but I didn't really remember much.., so it would be great if someone could perhaps give a link explaining this.

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

I don't quite understand ' if(n & (n — 1))' in the solution to A. I did some research and found that the binary representation of any number that is a power of 2 is 10 to the i and the binary representation of any (power of 2) — 1 is a string of 1's the length of the power of 2 — 1. So performing an AND operation gives a string of 0's in the case that the input is a power of 2 and a string of 1's and 0's in the case that it isn't a power of 2. 1. How can that be used in an if statement? If it's 0's (i.e. the input was a power of 2), is that the equivalent of it being false, and the contrary for a string of 1's and 0's in the case that it isn't a power of 2?

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

    Let's say $$$n$$$ is a power of $$$2$$$. Then, in base $$$2$$$, $$$n$$$ will look something like:

    $$$1000000\cdots000$$$

    and $$$n - 1$$$ will look something like:

    $$$0111111\cdots111$$$

    If we bitwise $$$AND$$$ these two, we obtain:

    $$$1000000\cdots000\space \&$$$

    $$$0111111\cdots111 =$$$

    $$$0000000\cdots000.$$$

    Which is equal to the representation of $$$0$$$, in base $$$2$$$. That is, if $$$n$$$ is a power of $$$2$$$, $$$n \space \& \space (n - 1)$$$ equals $$$0$$$. It's not hard to see this equals $$$0$$$ only if $$$n$$$ is a power of $$$2$$$. In all other cases, it will return some nonzero number. In a condition, $$$0$$$ evaluates to false, while any nonzero number always evaluates to true. Thus,

    if (n & (n - 1)) = if (false)
    

    when $$$n$$$ is a power of $$$2$$$, and

    if (n & (n - 1)) = if (true)
    

    when $$$n$$$ is not a power of $$$2$$$.

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

For problem E, I had a pure DP approach different from the editorial with no use of combinatorics, which I think works (at least I wasn't hacked yet if it wasn't the case).

Let's call $$$dp[i][j]$$$ the maximum number of followers we can get by choosing $$$j$$$ people in a prefix $$$[0,i]$$$ (the order of the array doesn't matter). Then let's also have another array $$$ways[i][j]$$$ which represents the number of ways to get to the amount of followers indicated by $$$dp[i][j]$$$. As a base case, it's easy to see that the maximum number of followers when choosing $$$0$$$ people is obviously $$$0$$$, and by that reason the number of ways of getting to that result is only $$$1$$$, so $$$dp[i][0] = 0, ways[i][0] = 1$$$ for all $$$i$$$. Then, we can calculate the value for the rest of the states: for each $$$i$$$ and $$$j$$$, $$$dp[i][j] = max(dp[i-1][j-1]+followers[j],dp[i-1][j])$$$. It means that the maximum amount of followers we can get by choosing $$$j$$$ people is already the calculated value for $$$j$$$ in the previous prefix, or choosing the blogger in position $$$j$$$ and adding the corresponding followers to the best way of choosing $$$j-1$$$ followers in the previous prefix. And $$$ways[i][j]$$$ is set to $$$ways[i-1][j-1]$$$ or $$$ways[i-1][j]$$$ in the first or second case respectively. Also, if we get to the case where $$$dp[i-1][j-1]+followers[j]$$$ is equal to $$$dp[i][j]$$$, then we add the corresponding ways from $$$ways[i-1][j-1]$$$ to $$$ways[i][j]$$$, applying $$$\%MOD$$$ to avoid overflow.

Just to be clear, I changed a little bit the wording from what I really implemented so it was easier to explain, but the idea is very similar. Here's the submission: 105415244

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

IMO easier solution for B: make a bool array DP length of the maximum possible n(1e6, as I remember) and fill it with false. Then make dp[2020]=dp[2021]=true. And for each number in [4040, 1e6] if dp[i — 2020] or dp[i — 2021] is true, than dp[i] = true. This basically means “if it is possible to reach B”. And then for each test case just print dp[n] ? Yes : No!

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

Anyone stuck at D , can watch this https://youtu.be/c7h4hUdXX1E

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

Hello I use greedy to solve problem D :)

if max1_type2 > max1_type1 + max2_type1:
     choose max1_type2
else
     choose max1_type1

you can see my code here : https://codeforces.me/contest/1475/submission/105374802

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

Thanks for contest, I thought D, F and G were very nice problems for beginners.

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

Can anyone share a more formal proof of F? Why are 2 cases handled in the solution? What is this 'times' variable?

Here is my current algorithm with logic:

  1. We have 2*N control variables: The flip of each row, and flip of each column. Each variable can be 0 or 1 (as the order doesn't matter)

  2. Elements in first row are determined only by the flip of 1st row and flip of all columns. So we look at all possible cases for this that can make first row of A equal to B. We do this for 2 cases: one where first row of A is flipped, one where it isn't. (note that the columns flipped in 2 cases will be complement of each other)

  3. Now, the flip or not of each column is fixed based on step 2. So only remaining variable is flip of each row other than 1st row. We can decide flip of each row by looking at first element in the row. Now all our variables are determined. At this point, A must equal B if the solution exists, in at least 1 of the 2 cases.

  4. But do we need to check both cases? In case 2, the flip of each row and column will be opposite to the flip of each row and column in case 1. Therefore for each(Row,column) pair (i,e. cell). The result will be the same in either case. Therefore, it suffices to check only 1 case.

  5. I follow this deterministic setup: where i first set first row of A as same of B by flipping columns (selecting the case where first row doesn't need flip). Then.I set first column of A as same of B by flipping rows. Then I check the other elements. Based on arguments in 1-4, this is sufficient.

Please help if I am overcomplicating this and there is a simpler understanding.

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

    Sorry for that I will not answer your post, instead I'll describe my solution, because I don't understand editorial.

    You should know from properties of XOR order does not matter, so we can reorder all operations into: perform all XORs of rows, and then all XORs of columns. Now, if any of operations is done twice then you can do nothing instead. Therefore you need to know for each of row and column do you need to perform corresponding XOR or not.

    For any cell in matrix after all XORs performed value will be changed or not. So, you can make matrix of "should I change value or not" — this is actually XOR of both matrices. You could also came up with this idea if you think of question "with what I should XOR to get from matrix A get matrix B?" Let's call this matrix C! So, C = A XOR B. Also C is "what cells should I flip?". This leads us to following equal problem: if you can make C matrix from zeroes matrix, then you can make B matrix from A matrix.

    How to figure out can we make C matrix from zeroes? Well... Let's split into two cases. Case 0: we don't need to XOR first row, then 0 and 1 in this row tells us: should we invert column or not. If any other row is same (identical, all 0 and 1 match first row) then after performing all columns XORs we get all we need. Otherwise, to be able to make it from zeroes, the only chance we have is to perform XOR of those rows, and to be able to make them they should look like our first row inverted.

    Case 1: we need to XOR first row, then 0 will tell that we should invert this column, and 1 tell us to not invert. And similarly, if we see same row somewhere else, then we just perform XOR of row and after all XORs of columns we get what we need. And for other rows, the only chance to make them is to NOT invert them, this means they should look like our firs row inverted.

    In both cases all rows of matrix C should consist of row 1 and inverted row 1. Therefore, solution is following: make matrix C, check that all rows is either identical to row 1 or identical to inversion of row 1.

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

Thanks to Stepavly and Supermagzzz. The contest went well, but unfortunately, it was delayed.

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

Can anyone Please help me,why I'm getting WA in problem c In case 3:( i think my idea is totally fine :)

105363911

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

For Problem G,why let the $$$\mathcal{O(\sqrt{n})}$$$ finding divisors algorithm pass!!!

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

For problem G, can someone please explain why is it incorrect to calculate dp in reverse. Why dp[i]=max(dp[i], cnt[i]+dp[i*j]) where i*j < 2e5 wrong? 105437344

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

Why I get 'uninitialized value usage' error https://codeforces.me/contest/1475/submission/105439474?

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

Hello everyone i just want to share my approach for problem C. I used maps to solve this problem. I stored the frequency of boys and girls and also pair of a boy and girl. Now if we want to calculate how many other couple can go with current couple. Say a is boy and b is girl we have in current couple. Then the number of couple that can go with a and b are Total couple-frequency of a in boys -frequency of b in girls + frequency of pair a, b. And do this for all couple and just divide the answer by 2 because we will count every couple 2 times.

105342359 Check this submission.

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

I just finished my virtual for the contest. I was analyzing my solution for F and found a mistake. But that code had already passed system tests.

105447780

The mistake I found is that I have done this operation:

(1<<1000).

This operation is not practically correct but somehow is giving correct result.

I think the System Tests are weak coz this solution should not have passed.

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

I'd like to propose a super simple solution for F. Just scan every 2x2 square and count the number of difference in A matrix and B matrix in matching positions, if there exist a 2x2 square in which the count of different numbers is 1 or 3, output NO, otherwise output YES. I got AC but hasn't been able to prove why. I just feel like different numbers can never come in a single square surrounded by same numbers or create an L shape.

Here is my solution, who can prove why this is correct? https://codeforces.me/contest/1475/submission/105455476

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

I tried D with priority queues by making different queues more important and less important respectively. But it gave wrong ans with some case.

This is my code https://codeforces.me/contest/1475/submission/105424077

Any suggestion in code or logic will be helpful.

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

For problem G, why can't we replace the N with max element in array a(for each test case)?

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

Comment Retracted

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

It's no need to check for two times in F actually. If there is a scenario where the first column must be flipped, just flip all rows and flip all columns, thus constructing a scenario where the first column does not need to be rotated. ( I use translate tools to type because I'm Chinese :( )

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

I am new to cp, in the problem 3 I was able to follow the editorial to the very end but I didn't get why we are printing ans/2 instead of ans

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

    we are counting each pair twice. We are fixing one pair and seeing all possibilities of second pair. But this first pair will also be counting again when its corresponding second pair is fixed.

    If you are doing a different solution where you are counting each pair only once, then you don't need to divide by 2

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

O(1) solution for Problem B

Link to the solution

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

please someone help, from last 2 months i started codeforces and i am not able to improve my performance yet i am a on a beginner level, I think the problems on codeforces are on little mathematics side. can anyonle please help me , and suggest me some resources for solving mathematical problems.

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

I am getting WA in fourth question, my approach is almost exactly as shown in this editorial code.

Can someone tell me where I am wrong? https://codeforces.me/contest/1475/submission/105487065

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

Editorial for 1475G - Strange Beauty is masterpiece of utterly bad editorial. Why? dp[x] is following. Here is formula. No explanations at all, why there would be numbers like that, why they indeed pairwise ok and so on.

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

How i can solve the problem D use DP !!

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

Can someone help me understand why I got TLE?

contest : Codeforces Round #697 (Div. 3) problem : C. Ball in Berland problem link : https://codeforces.me/contest/1475/problem/C

TLE solution : https://codeforces.me/contest/1475/submission/105347180

I resubmit same code. But I got AC AC solution : https://codeforces.me/contest/1475/submission/105608300

Why did this happen?

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

Stepavly In the problem C why did you first decrement the x and y and then increment a and b

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

in E , i am having trouble while taking mod

when i submitted without taking mod while calculating factorial then i got overflow. 106346070

but when i changed and gave mod while calculating factorial itself then , i got the answer as 0 106343305

pls someone help me out.

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

I feel problem-B more easier to use these codes.

include<bits/stdc++.h>

using namespace std; int main(){ int n; cin>>n; while(n--){ int k; cin>>k; int s; s=k/2020; if(k>=s*2020&&k<=s*2021)cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }

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

Problem C got a 100% binary search solution: 128061064 Idea: sort by boys increasing. Binary search to find first boy has larger value than the current boy. Then binary search again to find the number of boys paired with the current girl (by storing the position of all boys paired for any girl). Then add up.

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

Can you please explain why in problem E to calculate C(n, k), you need to use exponentiation. After all, it’s not in the formula

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

Simple solution for C(without graph && binarySearch) Here

Idea:- First store the cnt of all boys and girls independently, and then iterate from left to right and see how many pairs does the boy has in the forward direction , and same for the girl, because we don't want the same boy or girl to be in the second group, subtract their cnt from the length of the remaining block and add into your answer..