cry's blog

By cry, 16 months ago, In English

Special thanks to our hub of testuwuers for contributing solution explanations!

2044A - Easy Problem

Problem Credits: cry
Analysis: macaquedev

Solution
Code (Python)

2044B - Normal Problem

Problem Credits: Lilypad
Analysis: Lilypad, macaquedev

Solution
Code (C++)

2044C - Hard Problem

Problem Credits: cry
Analysis: macaquedev

Solution
Code (C++)

2044D - Harder Problem

Problem Credits: cry, Proof_by_QED
Analysis: macaquedev

Solution
Code (C++)

2044E - Insane Problem

Problem Credits: Proof_by_QED,Lilypad
Analysis: macaquedev, chromate00

Solution 1 (Binary Search)
Solution 2 (Intervals)
Code (C++)

2044F - Easy Demon Problem

Problem Credits: chromate00, Proof_by_QED
Analysis: DivinePunishment

Are you a python user and failing test 12?
Solution
Code (C++)

2044G1 - Medium Demon Problem (easy version)

Problem Credits: Lilypad
Analysis: macaquedev

Solution
Code (C++)

2044G2 - Medium Demon Problem (hard version)

Problem Credits: Lilypad
Analysis: Proof_by_QED

Solution
Code (C++)

2044H - Hard Demon Problem

Problem Credits: cry
Analysis: chromate00

Solution
Code (C++)
  • Vote: I like it
  • +98
  • Vote: I do not like it

| Write comment?
»
16 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

first ez

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

    actually, the "easy demon problem" was the hard one

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

    You can still use the same in-degree idea to solve g2 with minimal changes. You can see my submission.

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

      in your code you're basically looking for the maximum accumulated number of plushies for the vertices outside of a cycle?

          int n; cin >> n;
          in.assign(n+1,0);
          c.assign(n+1,0);
          set <int> nodes;
       
          for(int i = 0;i < n;i++){
              nodes.insert(i+1);
              int v; cin >> v;
              adj[i+1]= v;
              in[v]++;
          }
          queue <int> look;
          for(int i = 1;i<=n;i++){
              if(in[i] == 0){
                  look.push(i);
              }
          }
          while(look.size()){
              // cout << "in" << endl;
              int cur = look.front();
              nodes.erase(cur);
              look.pop();
              in[adj[cur]]--;
              if(in[adj[cur]] == 0){
                  look.push(adj[cur]);
              }   
              c[adj[cur]] += c[cur]+1;
              
          }
          int maxi = 2;
          for(int i = 1;i <= n;i++){
              // cout << c[i] << " "
              if(nodes.find(i) == nodes.end()){
                  maxi = max(maxi,c[i]+3);
       
              }
          }
          // cout << endl;
          cout << maxi << endl;
          
      
      • »
        »
        »
        »
        16 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        yeah basically

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

          Hi, could you explain why you add 1 in this line?

          c[adj[cur]] += c[cur]+1;

          Tried just initializing c with 1 for all nodes and then in the above line just not add 1, but it prints WA, so I don't really know what the intuition behind this is.

          Moreover, why add 3 here?

          maxi = max(maxi,c[i]+3);

          Thanks in advance

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

            In my code, I’m accumulating plushies starting from zero. If each plushy should initially start with 1, the first one would have 1, the second would accumulate 2, the third 3, and so on. However, I started with the first one at zero instead. To account for this offset, I’m adding 3 at the end instead of 2. The reason you would add 2 at all is because if the answer is always at least 2.

            Alternatively, if you initialize all plushies to 1 from the start, you wouldn’t need to compensate for the initial zero. In that case, you would add 2 instead of 3. AC

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

My Text Editorial (A-D)

Screencast

I was so stupid on C and D for real :(

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

I think this is one of the better div4's of recent history.

However, I was very disappointed that my editorial for problem A was edited. In fact, the four most important words in this editorial were removed. Initially, it said "This problem is trivial. For any n,...", but I have been censored, and the first sentence is no longer there.

Freedom of speech is a fundamental human right and I feel violated.

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

    cope and seethe

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

      I think there is some mistake in solution of 2044G1 (medium demon problem easy version). there must be maximum distance of any node to cycle instead of minimum distance. can you please check.

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

    No problem is absolutely trivial. What's trivial for you might not be trivial for someone else.

    Besides, saying "This problem is trivial ..." does not add anything in the value of editorial.

    • »
      »
      »
      16 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it +27 Vote: I do not like it

      are you enjoying being wrong? the word "trivial" just sounds nice

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

        Indeed, I am.

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

        Imo, it is used mostly as a condescending word. Idk why mathematicians are so obsessed with it. Sometimes I'd go to my professor for math help, and I'd show him what I needed help with, and he'd go "oh that's trivial" then tell me how to do it. Why would he say that other than to show off?

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

          Condescending, but also justified. The word "trivial" is reassuring, calming and transformative. I wish everyone used it more.

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

boy do i love getting TLE'd

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

Thank you codeforces team. This was my first contest here in cf and i was able to solve 3 problems i was trying to do 5 th question but didn't get how to work with time complexities for such high problems.. Overall beginner friendly.. Thanks in Advance Codeforces team.

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

omg my editorial for F made it uwu

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

Very creative problem names. (like my variable names)

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

Thanks for the awesome problem set -_-

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

I think, D is harder than E and F harder than G1. F — is the best problem, that i have seen on Div4 rounds)

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

problem f.............................

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

Thanks for the great contest!

Quick question, how are you supposed to round up without using ceil?

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

extreme demon where

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

Thanks for Lobotomy round , makes my brain FIRE IN THE HOLE.

Thanks for fast editorial , makes my brain WATER ON THE HILL.

As a Geometry Dash player , I'm quite excited to see my favourite game on codeforces.

As a codeforces round contestant , I'm playing Geometry Dash now and beat a easy demon :).

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

For G2, instead of topological sorting and DP, you can just find all spiders $$$i$$$ such that $$$r_i$$$ is in a cycle and do a simple dfs to find their subtree sizes.

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

g1 was nice

time was up while doing g2 :(

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

Can anyone tell me what is wrong with this code in E (binary search):

My code

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

    In binary search:

    int val = mid*powval;

    powval could be as big as 1e12, and mid could be 1e9. 1e12 * 1e9 = 1e21, and that number overflows long long.

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

      How do I fix that?

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

        You don't need powval to go up to 1e12. I think 1e9 is probably enough.

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

          But This code also doesn't work where powval goes up to 1e9

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

            I think it is strange that your binary search sets left = mid+1 if the condition is met, but sets right = mid-1 if the condition is not met.

            Usually, it is either left = mid+1, right = mid

            Or: left = mid, right = mid-1

            Take a look at your code and see if the error could be related to that.

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

              Doesn't work!

              Ok I will just ask ChatGPT then.

              Edit: That was no help.

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

                I've been looking at your code since then and I've managed to notice the following things.

                1) You would have to do two binary searches, one to find the first x that is valid and another one to find the last x that is valid. It seems like your code assumes x = l1 is always valid, which is wrong.

                2) You will not be able to binary search for those 2 x values directly on the interval [l1, r1], because it could happen that there are values x1 < x2 < x3 such that x1 is not valid, x2 is valid and x3 is not valid (i.e., binary search won't work on a non-increasing function). You will have to tighten that search interval, and the way you ensure your interval is scritcly increasing is by searching on

                [max(l1, (l2+powval-1)/powval), min(r1, r2/powval)],

                i.e., the left boundary becomes the first x that you are sure that works, and the right boundary becomes the last x that works.

                But now that you already calculated the first and last x that work, you don't even need the binary search anymore, and you already have the values you were searching for.

                Below is the link of the submission of this solution. I tried to modify your code as little as possible, and the only thing I modified is in between two comments.

                https://mirror.codeforces.com/contest/2044/submission/296764832

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

      can u also tell me whats wrong with submission

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

        You are always iterating with i from 0 to 31, even though k^n may increase much faster than 2^n (and the cap 31 you chose is for the worst case of k = 2). Indeed, it increases so fast that k^i easily overflows long long for some values of k > 2. All you gotta do is something like

        ll power = expo(k, i);
        if (power > r2) break;
        

        because you are always sure that whenever the power is bigger than r2, it will never fit in the interval anyways. That also ensures no overflow will happen, because the maximum r2 multiplied by the maximum k fits in a long long with no problem.

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

Problem with the MODE was really interesting!

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

Wasted time on F thinking about Binary Search, should have skipped to G1. Good learning.

Edit: F is a beauty cud never think thru this approach

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

Help me With Problem D, please. Is there another way to solve this without using a set?

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

    You can use an array as an alternate hashset. Conceptually you cannot avoid a set because you need to mark appeared numbers.

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

https://mirror.codeforces.com/contest/2044/submission/296719812

If anybody could tell me the exact case where my code fails, I would really appreciate it! Thanks

PS- This is based on the intervals logic that the author had shared, however I am not able to figure why is it giving a wrong answer on 121st case on Test 2. (1 instead of 0)

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

Someone tell me how to solve D if there was only allowed to use the elements of vector a only instead of 1 <= b[i] <= n

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

why not to use ceil function?

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

    Ceil has some precision issues. This code shows an example of that:

    long long a = 1000000000000000000;
    long long b = a-1;
    long long withCeil = ceil(1.0*a/b) * b;
    long long withoutCeil = (a + b - 1) / b * b;
    cout << "With ceil: " << withCeil << endl;
    cout << "Without ceil: " << withoutCeil << endl;
    
    

    Output:

    With ceil: 1000000000000000000                                                                                          Without ceil: 1999999999999999998
    
  • »
    »
    16 months ago, hide # ^ |
     
    Vote: I like it +6 Vote: I do not like it

    You risk precision loss by using ceil. Especially the round is open hacked, and your risk doubles.

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

Can you explain to me, plz, how to solve F without "yesterday my math teacher showed me some stuff so i will create a problem for that, it will be so cool" observation in the editorial?

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

    "yesterday my brain showed me some stuff so you will have to solve a problem for that, it will be so cool" is what all codeforces rounds are about, cope and seethe

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

      Man, I don't wanna argue about was it difficult to get this idea or not. It's a solid problem no matter what. But if you look at the solutions of participants who had 2 hours instead of several days, you'll see that most of them used another approach that I don't understand. So I want to get it

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

    the 2nd part of the editorial is kinda just extra for people who didnt get it. the B=SumA*SumB is obvious, and if you dont get that, idk. then you just realize that when you make an entire row 0 and an entire column 0, you essentially just make SumA smaller by a[c] and SumB smaller by b[r], so new B=(SumA-a[c])*(SumB-b[c]), and then its clear you can just iterate with divisors. this is the best i could explain without the "math stuff". but i also think your comment is very stupid, since cp is just math with computers

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

      Calling my comment "very stupid" doesn't do you any credit but only shows your rudeness. Also it shows that you're rushing in without figuring things out. I asked for another solution than in editorial. And explanation in editorial is clear for me.

      Plz, problemsetters, stop responding for my comments here, because I don't want to argue with you, I want to get another approaches to this problem if there are any. And if my first comment was offending for you, I'm truly sorry, I'm not here for a verbal sparring, and I was genuinely considering my words about math teacher as a mini-joke.

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

anyone use scc for G problem?

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

Loved Problem F Solution But during round the thought of factoring the equation didn't come to my mind. so i considered the equation to_remove = total_sum_of_grid minus x and

to_remove = ai*(sumof_b) + bj*(sumof_a) — ai*bj

can we apply binary search on this equation by fixing i on sorted array a and then j on sorted array b?? is it possible to get solution?? i tried but couldn't make it right****

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

    Thanks for bringing this up, I was thinking the same thing! Unfortunately the ai*bj term is really annoying, I don't know how you could binary search with that in there.

    • »
      »
      »
      16 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it +1 Vote: I do not like it

      we can first fix the index of array a by binary search on a and in the while loop we can manupulate the cofficient of bj as sum_of_a minus a[i] (as we already fixed i) so that we get rid of ai*bj term and now we can binary search on j

      i tried it but it is not giving correct answer for sample tests also

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

For H the following equation might be of help for finding the answer for a general submatrix

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

Anyone who wants to solve array version of the H problem.

https://www.hackerrank.com/contests/nitc-icpc-series-1/challenges/array-queries-1

I would suggest, first solve array version, then try to solve the grid one.

Also, in the array version, there is one more problem, where you can do point-update and range query using segment tree.

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

loved g1 and g2. solved g1, didnt have enough time left for g2. great contest!

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

CYAN, YAY!

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

Hey, could somebody look at this submission for problem H and tell me why it gives TLE? 296778260 I'm at a loss for words... I already got AC but ONLY after implementing a custom readInt function in C and turning it in with C++20 (GNU C11 gave TLE with exactly the same code).

What am I doing wrong? Firstly, I expected the C version to be faster, but in always TLEd on test #2, while the C++20 code always TLEd on test #4. On my computer it runs fine even with a randomly generated max n/max q tests.

My main suspicion is the fact that I use unsigned long longs or perhaps I'm going out of bounds at some point. I highly doubt it has anything to do with cache misses.

  • When I tested with C++17, it passed with a max runtime of 2 seconds...????
  • »
    »
    16 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Figured it out...

    The major bottleneck here is the input speed. Somehow, scanf ends up being slower than cin... How? I do not know. I always assumed scanf was faster than cin.

    • Using scanf/printf, I got TLE with C++20 and ~2.2second with C++17.
    • Using cin/cout, I achieved a speed of ~1.5 seconds with relatively clean code.
    • Using io buffering on the input/printf, I was able to achieve a speed of around 560ms.
    • Finally, buffered IO with a few cache optimizations provided a time of 421ms.

    I also find it rather strange that when submitted with C11 (with almost identical code to the 4th stated submission), it actually becomes significantly slower (~765ms). I have no idea why. Also, why would the C++20 version with scanf/printf give TLE and the C++17 version not? Finally, since when is cin faster than scanf? Was I just oblivious to this fact or...? Additionally, using clang with -std=c++20 on my computer cin/cout end up begin WAY slower (at around 7.5s) for the random stress test, whilst scanf/printf do fine (at around 700ms).

    Takeaways:

    • I guess use cin/cout over scanf/printf for input sensitive problems at least on codeforces.
    • If needed (probably never) use super fast buffered io.
»
16 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

i can't believe there is easy medium demon and hard medium demon. just like gepmetry dasj

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

Geometry dash????

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

idk your thoughts on problem F, but to me it's like a 1400, at most 1500. Fun problem though!

»
16 months ago, hide # |
Rev. 4  
Vote: I like it +31 Vote: I do not like it

This can be implemented using a simple map or boolean vector for faster computation, although such optimization is not required for this problem.

I think you overlooked the worst case for the "map" solution. My hack idea for F is to make the slowest case for such solutions, and it caused tons of TLEs.

Let's say $$$SA$$$ is the set of $$$SumA-a_i$$$s and $$$SB$$$ is the set of $$$SumB-b_i$$$s. The most basic way to implement this solution is for each $$$i \cdot j = x$$$ ($$$1 \le i \le \sqrt{x}$$$), to check if any of the following conditions is true:

  • $$$i$$$ is in $$$SA$$$ and $$$j$$$ is in $$$SB$$$
  • $$$j$$$ is in $$$SA$$$ and $$$i$$$ is in $$$SB$$$
  • $$$-i$$$ is in $$$SA$$$ and $$$-j$$$ is in $$$SB$$$
  • $$$-j$$$ is in $$$SA$$$ and $$$-i$$$ is in $$$SB$$$

The order of the four conditions doesn't matter, as long as the answer is "NO", since it will have to verify all of them anyways. However, an important thing here is that each condition actually contains two checks, concatenated by an "and". This means that such codes, in most languages, will be short-circuit-evaluated. So, if none of them is in $$$SA$$$, it will perform only the first check of each condition and move on, without checking the $$$SB$$$ part, resulting only in 4 checks each time.

Therefore, my idea was to query the number with most number of divisors, while letting $$$i$$$, $$$j$$$, $$$-i$$$, $$$-j$$$ are always in $$$SA$$$, while none of them are in $$$SB$$$, and making sure that all $$$2 \cdot 10^5$$$ elements for each array are distinct. This way, I was able to force these solutions to perform 8 checks each time, and as expected, many of the solutions that took a little more than 2 seconds in the original tests, well-exceeded 4 seconds on this hack test.

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

    bruh

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

    How did you generate such a large test case(q<=1e4 and x<=1e5) such that all of i ,j,-i,-j are present is SA and not in Sb?

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

      You can refer to this hack generator.

      Basically, we want to aim for $$$SumA = SumB = 0$$$. That'll make it easy for us to work with the elements, because then each element itself would just be the element of $$$SA$$$ and $$$SB$$$ (to be precise, it's the negative value of it but it doesn't really matter since positive and negative are symmetric in this problem).

      To do this, we can first put $$$n-1$$$ elements to an array, and then insert the negative of the sum of these $$$n-1$$$ elements. Since we can only use elements within range $$$[-n, n]$$$, the sum of the $$$n-1$$$ elements has to be within that range too. The strategy for this is simple. While we're making these $$$n-1$$$ elements, we pick either a posivie random value or a negative random value based on the current sum. If the sum is negative then we put a positive value, and vice versa. We just need to hold an additional set to check if the value is duplicated. If the $$$n$$$-th element is to be duplciated, we can remove the $$$n-1$$$-th element and try choosing it again.

      The only difference between $$$SA$$$ and $$$SB$$$ is that, for $$$SA$$$ we start by inserting every divisors of $$$x$$$ first, while for $$$SB$$$ we avoid such values while picking random values.

      Also, you can easily do this without randoms (like you can just put $$$k$$$ and then $$$-k$$$ to keep the sum $$$0$$$), but I prefer using random because for some reason sets and maps are fast on most of data that have patterns, and is easy to modify the seed to generate another similar test.

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

    Hello djm03178,

    The submission using map resulted in TLE, as seen here: 296746721.

    However, the version using unordered_set passed successfully, despite performing 8 checks: 296883645.

    Given that the worst-case time complexity of unordered_set operations is O(n). Could you clarify why this difference occurs and how unordered_set is still more efficient in this case?

    Thanks!

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

      Wow. Changed my solution from map to unordered_map and now it passes. Did no one try hacking hash tables? That's crazy.

      In general, unordered_set and unordered_map take O(1) time. But generally there are tests designed against these data structures which make them take O(n) time. Not the case here tho

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

        Yeah I thought the same , I just wanted to know if there are any other reasons , and also when it is beneficial to use map/unordered_map, unordered_set

    • »
      »
      »
      16 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it +4 Vote: I do not like it

      I think it's impossible to hack unordered_set in this problem. You can read https://mirror.codeforces.com/blog/entry/132929 . To be short, you need a wider range for the elements than just $$$n$$$ to hack them. We can technically make something that makes the insertion part a little slower, but I'm not sure if we can do much with slowing down the query part at the same time then...

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

        Actually, it could be possible, but I'm not too sure. For example in C++20, we can insert multiples of $$$541$$$ into $$$SA$$$ and $$$SB$$$ in both positive and negative directions. Then repeat querying something that will find a lot of numbers that are multiples of $$$541$$$, and from what I found it is $$$194760$$$, which will search $$$48$$$ of them from each of $$$SA$$$ and $$$SB$$$ in total. Looking up other values than multiples of $$$541$$$ will take negligible time.

        I don't know how exactly we can control the insertion order so that all elements we try to find will take time as closely as looking up all $$$541$$$ elements, but there likely is a way for this. For $$$SB$$$ we would just need to exclude these values and they will always check all $$$541$$$ elements.

        If we naively calculate this, it will be $$$2 \times 541 \times 48 \times 50000 \simeq 2.6 \cdot 10^9$$$. However, the constant for resolving hash collision is relatively tiny, so I can't be sure if this would be enough to exceed 4 seconds.

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

      Ok, I tried it and it looks possible: https://mirror.codeforces.com/contest/2044/hacks/1102766/

      We probably just need to find a way to adjust the insertion order to slow down the finding process on $$$SA$$$.

      • »
        »
        »
        »
        16 months ago, hide # ^ |
        Rev. 5  
        Vote: I like it +17 Vote: I do not like it

        I made it: https://mirror.codeforces.com/contest/2044/hacks/1102768

        At first, the plan to force 8 checks wasn't working, because $$$i$$$ and $$$-i$$$ were not in $$$SA$$$ because they were not divisible by $$$k$$$, and therefore it did only 6 checks, skipping two $$$SB$$$ checks. So, instead I sacrificed a bit of hash collisions to insert all $$$i$$$'s and $$$-i$$$'s too, so that all $$$SB$$$ checks will be performed.

        I don't know if it's always this simple but inserting the target elements first made the search slow. Reversing the $$$a$$$ array makes it really fast.

        This was quite challenging, but man, it's surprising that we made use of the $$$\mathcal{O}(\sqrt{n})$$$ version of the hash hack.

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

      I am kind of late but i think the main problem with your solution is how you are using maps, like every time you do something like this "mpa[d1]" if d1 doesn't exist in the map c++ will create that key and is going to assign it the value 0 by default, the problem here is that it means that every time you access the map in a position that didn't exist before you're increasing the map size and by doing so on each step your map queries will get slower after each query.

      I think that with a good construction of queries your maps can eventually have every possible number between -2e5 and 2e5 in addition to probably 2e5 values greater than 2e5, so yeah, probably that's the reason why you were receiving tle, you can look at my solution where i used .count to avoid that problem.

      My solution (16 months late but maybe it will help someone in the future i guess)

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

We missed Insane and Extreme demons from the problem set I guess

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

I think there is some mistake in solution of 2044G1 (medium demon problem easy version). there must be maximum distance of any node to cycle instead of minimum distance. can someone please check if i am right or wrong.

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

Could anyone help me with my code? https://mirror.codeforces.com/contest/2044/submission/296745472 codeforces said it has runtime error on test case #10 . But when I try the code and the test case #10 on my pc, also on ideone ( https://ideone.com/eVDhX0 ), I found no error. Is there a fix for my code? I just can't find the error.

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

F was a type of problem I have never solved till now. Very innovative! Thanks for the problems.

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

Problem E can also be solved using the technique of solving Diophantine equations. Indeed, iterate over a sequence $$$(k^0, k^1, k^2, \dots, k^n, \dots)$$$ while $$$k^n \leq 10^9$$$. On each iteration we need count how many integer solutions $$$(x, y)$$$ the system

$$$ \dfrac{y}{x} = k^n, \quad l_1 \leq x \leq r_1, \quad l_2 \leq y \leq r_2 $$$

has and sum these values to obtain a final answer. Thus, it is necessary to calculate how many solutions the Diophantine equation $$$x k^n - y = 0$$$ has that satisfy the inequalities $$$l_1 \leq x \leq r_1$$$ and $$$l_2 \leq y \leq r_2$$$. This can be done using the standard technique (e-maxx.ru version has bugs). Implementation: 296834109.

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

TLE after systest (F).

O(Q * sqrt X * log(N * M) + NlogN + MlogM)

What's going on?

#include <bits/stdc++.h>
#define MAXN ((int)2e5+10)
#define MOD ((int)1e9+7)
#define int long long
#define endl "\n"
using namespace std;

void solve() {
    int n, m, q;
    cin >> n >> m >> q;

    vector<int> a(n), b(m);
    map<int, bool> ra, rb;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ra[a[i]] = true;
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        rb[b[i]] = true;
    }

    int A = accumulate(a.begin(), a.end(), 0ll);
    int B = accumulate(b.begin(), b.end(), 0ll);

    while (q--) {
        int x;
        cin >> x;

        bool found = false;

        for (int i = 1; i * i <= abs(x); i++) {
            if (x % i == 0) {
                int d1 = i, d2 = x / i;

                if ((rb[B - d1] && ra[A - d2]) || (rb[B - d2] && ra[A - d1]) || 
                    (rb[B + d1] && ra[A + d2]) || (rb[B + d2] && ra[A + d1])) {
                    found = true;
                    break;
                }
            }
        }

        cout << (found ? "YES" : "NO") << endl;
    }
}

signed main() {
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}
  • »
    »
    16 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    $$$O(Q \cdot \sqrt X \cdot log(N \cdot M))$$$ or just $$$O(N \cdot \sqrt N \cdot log(N))$$$ in general is a very evil time complexity, and in your case you have a very bad constant factor on top of that.

    Either you have to get rid of std::map in your solution, or store answers for values of $$$X$$$.

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

    ra[X] insert a pair {X, false} into ra if ra does not contain X as key. It greatly degrades performance.

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

the problem F gives TLE while we use map on test 25 but gets accepted with unordered_map. I know unordered_map operations are of O(1) while map takes O(logn) but is there no case of collision in unordered_map?! I dont know much about collision but it gets hacked so i prefer using map instead of unorderd map. can anyone explain?!

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

    Clearly no one made a test case that generates collisions, else unordered_map would be O(n) and it would easily TLE. The regular map giving TLE is because your solution is probably O(n * sqrt(n) * log(n)). What you can do to solve that is simply use a vector of size 1e5 to check if a given sum exists, and that leads you to a O(n * sqrt(n)) solution, without having to worry about hash collisions.

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

.

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

    I don't know if it is your case, but usually problem setters set some test cases that make your hash functions collide a lot. Since Python's set is hashed, that is probably the case, and you should solve it with another technique / data structure.

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

can anyone post any binary search solution for problem E

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

Can anyone tell why my code is giving TLE for question G1?

Java Code G1
Converted C++ code G1
»
16 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

296870648

Unsure why my submission TLEs for Problem F. I tried to implement it based on the explanation of the editorial, any tips appreciated. Thanks!

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

Why my code isn't correct (E) ? Thanks in advance Code

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

I have a question about Problems like problem F
I think my analysis was good and found the key formula
$$$X = B - (b_i \cdot \text{SumA} + a_j \cdot \text{SumB} - a_j \cdot b_i)$$$
Also, realized that the path to the solution is to somehow manipulate these terms to make the search task easier
the problem is I have no clue how to do it, so made some random attempts then read the editorial
Are there any techniques or solving methods or whatever to get better at such a task except solving many similar problems and accumulating ideas?

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

    I encountered problems such as this one before, so i am pretty sure you just need to gain more experience. in these problems where you simplify formulas until you can get something simpler, its all about being persistent and writing down everything and also breaking everything down. Im sure if you also just broke down B into (SumA)*(SumB) you could have gotten it, but its an experience you can learn from regardless

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

F is just WOW!!!

nice, got to learn new thing. But it was little bit hard, since the time complexity was also tight.

Using Map it gave TLE, but Using unordered_Map it passed.

But unordered_map can also get collision and access time can become very worse leading to TLE, but here it is not the case :) DivinePunishment

chromate00

can you please suggest me what should one ideally do in this case?

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

    My submission using maps passed, but also barely as well. Unordered map shouldnt pass, and im surprised there isnt a hack for it, but regardless i suggest using 4 boolean vectors to be safe.

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

      It's not always easy to hack unordered_maps. Check https://mirror.codeforces.com/blog/entry/137306?#comment-1228509 this comment. I tried hacking it with the same test but for some reason it only took 3.3s, and I don't know exactly why. It probably has something to do with the insertion order, but I'm not too sure about this.

      The general situation where unordered_map is absolutely bad is when the range of elements is large enough and is independent of $$$n$$$. The time complexity of a single insertion or search can only be at most $$$\mathcal{O}(\sqrt{n})$$$ when the range is only $$$\mathcal{O}(n)$$$, and its constant is relatively small. Read https://mirror.codeforces.com/blog/entry/132929 for more details.

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

in problem G1 how to check the longest path from node to it's cycle in details ?

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

    You can copypaste some topological order algorithm (like Kahn's algorithm) to know what vertices belong to a cycle. Then, reverse the adjacency list and throw a dfs from each vertex that belongs to a cycle (just don't go to neighbors that also belong to a cycle). The largest depth any dfs reaches is the maximum distance a vertex has to a cycle.

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

for the F question, i think i have implemented what the tutorial says... i am still getting a TLE on testcase 3. can someone tell how to optimize the solution? I have attached the submission for referrence...296889036

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

    Inside your "queries loop" (O(q)), you are building a whole set from the two vectors:

    //storing vectors a and b in sets, for effecient searching.
    set<ll>aset(all(a)), bset(all(b));
    

    That line, by itself, has complexity O(n log n), leading to a total complexity of O(qn log n). That complexity clearly TLEs given the problem constraints.

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

      Apologies for that oversight.. i improved that by creating a unordered map apr and bpr outside the loop, which tells if an element is present in a and b..

      i am still getting TLE :((

      296890768

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

        Now, your std::accumulate call is O(n), but since you are doing it inside the O(q) loop, its complexity becomes O(q*n). Since "dela" and "delb" don't change in between queries, I think it would be a good idea to compute them outside of the "queries loop", therefore decreasing the complexity of your algorithm.

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

Does a wrong submission decrease rating/ranking?

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

why am getting tle in problem f and my timecomplexity- q*sqrt(abs(x))*(logn +log m)

submission

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

The TLE is the real demon in problem F.

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

2044F — Easy Demon Problem , we know that beauty of matrix is always Sum_A*Sum_B, if we make ai=0 and bj=0, beauty will be (Sum_A — ai)*(Sum_B — bj). I think it's a simpler way to think.

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

Whoever got "Wrong answer on test 19" on problem D like this person (https://mirror.codeforces.com/contest/2044/submission/296678418) should be banned. It's code I leaked and hacked. There are hundreds of people to ban easily.

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

Problem F can be translated into a "product query" problem. Given two sets of integers $$$A$$$ and $$$B$$$, check if it is possible to find $$$a \in A$$$ and $$$b \in B$$$, such that $$$a \times b = x$$$.

In the original problem, $$$x$$$ is at most $$$2 \times 10^5$$$, so you could pre-process every possible multiplication that does not exceed the upper bound in $$$XlogX$$$, where $$$X$$$ denotes the upper bound of $$$x$$$. How do you approach this problem if $$$x$$$ can be very large? Say, $$$x \leq 10^{12}$$$.

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

For Test case 1 of problem E, I get 7 instead of 12. Can someone please point out my mistake.

Here are the values i get for (x, y)

(2, 4) (2, 8) (3, 6) (3, 12) (4, 8) (5, 10) (6, 12)

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

    you need to make ints long long and some small changes to code work faster like reserve and unordered_map 325272337