koderkushy's blog

By koderkushy, history, 6 months ago, In English

2158A - Suspension Idea & Solution: kingmessi

Hint 1
Hint 2
Hint 3
Tutorial
Solution
Bonus

2158B - Split Idea & Solution: kingmessi

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Tutorial
Solution

2158C - Annoying Game Idea & Solution: kingmessi

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial
Solution
Bonus

2158D - Palindrome Flipping Idea & Solution: kingmessi and abc864197532

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Tutorial
Solution

2158E - Sink Idea & Solution: kingmessi

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Tutorial
Solution

2158F2 - Distinct GCDs (Hard Version) Idea & Solution: koderkushy

Tutorial
Solution (Python)
Solution (C++)
  • Vote: I like it
  • +83
  • Vote: I do not like it

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
6 months ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

what an amazing solution for problem D!

Thanks!

»
6 months ago, hide # |
 
Vote: I like it +17 Vote: I do not like it

For problem F2, it may be easier to randomize set X instead of using a formula. It's enough to take the first 21 prime numbers and multiply 11 random ones among them: 351258817

»
6 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

For anyone who needs just an add-on to understand the editorial for B a little better

I really got cooked by this problem but finally came up with a solution that happened to match the editorial 2 hrs post contest lol

Spoiler
  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    code? btw easy explanation:)

    • »
      »
      »
      6 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it
      Code
  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    What if we have an odd number of odd fillers? would the answer be 0 then because there is no way to balance it out? if it doesnt have to be 0 then could you pls provide a counterexample? tysm

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    Screenshot-2025-12-01-7-14-08-AM []!

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Bro thanks I had been stuck at the tutorial for 2 hrs . Your approach saved me

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You saved my brain

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

My solution for D is exactly similar to the editorial lol

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Here, suppose there is a case where s -> t is not reached within 2n operaitons through the method talked in editorial. How are we sure that there would not exist any other set of operations that may lead s to t within 2n operations

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

My solution for C didn't pass because of +1 :(

For the last 30 minutes I was writing SEGMENT TREE OF ALL THINGS.

Quite sad, but it's alright because I absolutely LOVED the contest, thanks a lot for the good problems

»
6 months ago, hide # |
 
Vote: I like it +146 Vote: I do not like it

When the question suggests outputting -1 for impossible case, but there isn't any in input example.
Doakes Meme

»
6 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

A new node idea in E is quite amazing. Nice problem!

»
6 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Got a new logic for Pb B without using mod 4 stuff. Check my solution here https://mirror.codeforces.com/contest/2158/submission/351225505 (It is not clean though, I was in a hurry to submit XD )

»
6 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

In C after the idea of differentiating between $$$k$$$ odd and even you can also just use Kadane's Algorithm: Maximum Subarray Problem.

See here 351210965. With even $$$k$$$ we just use Kadane's algorithm. With odd $$$k$$$ we add another state: The best possible sum, assuming we already used Alices one move to add $$$b_i$$$ to one number.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can someone tell the issue in my code

351260725

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You are assuming that the answer for the 'odd k' cases will lie in the subarray which includes the subarray found using kadane's algorithm, but this is not necessarily true. Below is one of the cases when this would fail.

    1
    4 5 
    3 -5 2 0
    3 2 4 5
    

    The answer that your solution would give is 6 (includes the 0th index only) while the actual solution is 7 (the subarray from 2nd to 3rd index along with b[3]).

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

-106

»
6 months ago, hide # |
Rev. 2  
Vote: I like it +6 Vote: I do not like it

In Problem F2, I wrote a randomization program and found 100 numbers with pairwise distinct gcd values.

randomization program

The probability of this randomization program succeeding was low, but I was lucky enough to stumble upon one such set of numbers.

After obtaining these 100 numbers, the construction method for this problem is the same as in CF1981D.

The submission

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone tell me the problem with my code for D?:

Code
»
6 months ago, hide # |
Rev. 9  
Vote: I like it 0 Vote: I do not like it

Problem D can also solve through bfs .

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    Solution from editorial looks complicated. Alternate solutions are easier to come up to, though probably a bit harder to implement.
    You already mentioned the BFS, I want to add that brute force is also possible.
    So, we move from left to right, transforming current element of s into t, until 4 elements are left (4 is a guess, coming from fact that n is >= 4 by problem statement).
    It can be done greedily. Say, current element of s is 1 and in t it's 0. Let's find next closest 1 in s. Interval between current element and that next 1 will be palindrome (because there are 1's on sides and all 0's in the middle). If there are no more 1's in s, it means all elements to the right are 0's, so flip them all since they form a palindrome, now we have all 1's, so flip again to get all 0's.
    Thus, in at most 2 * (n — 4) operations we transform first n — 4 elements.
    For the remaining 4 elements use either bruteforce of BFS. There are only 2^4 * 2^4 combinations, so we can just try all of them and make sure that all can be done withing 8 operations.

    • »
      »
      »
      6 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      how did you calculated that " There are only 2^4 * 2^4 combinations ".

      • »
        »
        »
        »
        6 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        There are 2 ^ 4 binary strings with 4 elements, because each element has 2 possible values (0 and 1). We have 2 binary strings containing 4 elements each, thus for each of 2^4 possible values of 1st string, there 2^4 combinations of the second string. Or we can view 2 strings of lenght 4 as a 1 string of length for. Thus we have 2^8, which is equal to 2^4 * 2^4.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

How does solution for D ensure that we're doing it in minimum operations?

»
6 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

Did any one write recursive dp solution to problem C?

Here is a version

Spoiler
»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

the question C's solution is wrong because for this test case:

1 3 3 2 -7 3 1 -10 3

alice will not add -10 but subtract it so with your solution the final array is 2 -7 6 ans: 6 but the answer should be 2 3 3 ans: 8 i think you should update it and if can update the solution of this question @kingmessi

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is the solution given in the editorial for problem C is correct ??

Because if I have

n=1,k=1 means just 1 move and 1 size array made by Alice only

and if the array is

a={1}, b={-4}

if i am using the editorial solution it is giving me output -3 but it should be 5 naa i think because we can do either a[i]=a[i]-b[i] or a[i]=a[i]+b[i] so whyy can't we do here a[i]-b[i] in editorial it is always doing +b[i]

or i am incorrect thing is some other direction can any one please help and tell me the reason

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone please tell me why this solution is not working ?

My Submisson

I have used the same concept similar to editorial 1 kadane's algo logic but is failing i have also added proper comments there can anyone please tell me why it is falling it will be very helpful for me

»
6 months ago, hide # |
Rev. 3  
Vote: I like it +3 Vote: I do not like it

D: Repeat: flip the substring from the first 1 to the next 1.

When there is only one 1 left, it can be zeroed out in at most 3 flips.

  • »
    »
    5 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    your sol was wayyy easier to understand,its so cool wtf,tysm for sharing,btw it can be zeroed out in just 2 flips dont need 3

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

simpler method to get "11..."

// this is code
vector<pair<ll,ll>> cal(string s){
    vector<pair<ll,ll>> p;
    if(s[0]!=s[1]){
        if(s[1]==s[2]){
            s[1]=s[2]=s[0];
            p.push_back({1,2});
        }
        else{
            if(s[2]==s[3]){
                p.push_back({2,3});
                p.push_back({1,3});
                s[1]=s[2];
            }else{
                p.push_back({1,3});
                s[2]=s[1];
                s[1]=s[3]=s[0];
            }
        }
    }
    for(int i=1;i<s.size();i++){
        if(s[i]!=s[i-1]){
            p.push_back({0,i-1});
        }
    }
    if(s[s.size()-1]=='0')p.push_back({0,s.size()-1});
    return p;
}
»
6 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Why no test cases in D with output = -1 ? Many codes could be hacked !! Like
Input:
1
2
01
10

Output:
-1

»
6 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

https://mirror.codeforces.com/contest/2158/submission/351409705 why this fails can anybody give testcase?

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    By setting curr = a[i] + b[i], you already ensure that atleast one element is taken which allows empty left and right subarrays. So change curr += left[i - 1] to curr += max(0LL, left[i - 1]. Make similar change to the next line of your code.

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

What an amazing question f2. My blog https://mirror.codeforces.com/blog/entry/148841 contains a proof-style explanation of F2, which may be helpful if you like formal proofs :)

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

What would be the expected rating of D?

»
6 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

is the checker in problem D wrong? cause in test 1 even when my code print valid operations, the checker log still is "wrong answer Invalid operation: substring is not a palindrome (test case 1)", could anyone check the code for me? here is my code 351716981

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

like the solution to B

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Wow!Amazing problemn for D!

»
6 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Solution for D — wow!

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
Hey everyone I'm looking for a cp peer group, if anyone can let me join in do let me know. Thank you
»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Nowadays B is getting more hard and hard right?

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Question B is great! I totally get it now.

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

really great editorial

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Third problem can be more complex if u just add -1e6<=bi<=1e6

»
6 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

D was so good. i feel proud of myself that i could come up with the same solution given in the editorial. (though it took me half an hour to think and another half to code :))

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Could someone please explain why in C we need to do L[i] + R[i] + a[i] + b[i]? Why do we calculate max subarray sums for either sides of i?