cry's blog

By cry, 5 weeks 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: SkibidiPilav

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

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

first ez

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

    when the rating will publish

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

      in 20 min maybe, usually the rating publish before 24 hours pass since the contest started.

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

        It was my first contest so when I get rating do I need to participate more 4 contest?

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

          Just wait 10 min and you will get rating. Any body need to wait after finishing any contest before he gets any rating

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

            i haven't got any rating. What could be the reason for this?

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

          Same bro I also didn't get any rating

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

            You might have registered as Unrated participant

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

              means??

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

                Some contests allow unrated participants.

                So you might have registered as unrated. There comes a checkbox while registering for the contest

»
5 weeks ago, # |
  Vote: I like it +2 Vote: I do not like it
  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

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

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

    nice sol and walkthrough, thanks!

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
        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;
          
      
      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        yeah basically

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            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

          • »
            »
            »
            »
            »
            »
            5 weeks ago, # ^ |
              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

»
5 weeks ago, # |
  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 :(

»
5 weeks ago, # |
  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.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +73 Vote: I do not like it

    cope and seethe

    • »
      »
      »
      5 weeks ago, # ^ |
        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.

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
      Rev. 2   Vote: I like it +27 Vote: I do not like it

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

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

        Indeed, I am.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          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?

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            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.

»
5 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

boy do i love getting TLE'd

»
5 weeks ago, # |
  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.

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

    wait for next round. its gonna be div(1 + 2). xd xd xd. Fireworks.

    All the best.

»
5 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

omg my editorial for F made it uwu

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

    whatsup bro?

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

    how solve easy demon,I just have thought sum-a*sumb-b

  • »
    »
    4 weeks ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    297766203 This submission got wrong answer 38th testcase

    297765680 This submission got tle.

    What am I doing wrong??

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

      Even I am having wrong for tc 38th pls help

      297246968

      Sorry I cant help you with anything but I guess using vector is creating issue as thats the only diff I see in your submission but I dont think so it should create any issue as I have also used that ....

      Any help would be appreciated!

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

    Can you help me debug my solution. 300958196 My approach seems to be the same as the editorial but I seem to get WA on test 38. Seems to be some edge case

    • »
      »
      »
      4 days ago, # ^ |
      Rev. 2   Vote: I like it +3 Vote: I do not like it

      ive tried making everything into ll and tried all the c++ compilers and none of it works. i even get tle when putting set instead of ordered set, which is weird. really confused tbh. the logic seems correct as well, so idk

    • »
      »
      »
      4 days ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      ah okay i found it. you get overflow when using accumulate because you put 0 instead of 0ll. i get tle on test 39 now so either switch to boolean array or use sets less somehow.

»
5 weeks ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Very creative problem names. (like my variable names)

»
5 weeks ago, # |
  Vote: I like it +8 Vote: I do not like it

Thanks for the awesome problem set -_-

»
5 weeks ago, # |
  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)

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

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

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    You do q * n * m operations on your code. This is very big count.

»
5 weeks ago, # |
Rev. 2   Vote: I like it -10 Vote: I do not like it

https://codeforces.me/contest/2044/submission/296746911

can anyone tell me where my logic is inncorect?

here i check if element greter than 1 then

i b[i]=a[i+1],that means

1 1 1 2 should be 1 1 2 2 bcz here for i=2,a[i]=1 and this one is repated

so b[i] = a[3] i.e v[2]=2 right?

»
5 weeks ago, # |
  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?

  • »
    »
    5 weeks ago, # ^ |
    Rev. 2   Vote: I like it +23 Vote: I do not like it

    $$$\lceil \frac{n}{i} \rceil = \lfloor \frac{n+i-1}{i} \rfloor$$$

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

    You could use the modulo operator if you have floor division. ceil(a/b) = floor(a/b)+(a mod b != 0)

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

    what is the problem with ceil function?

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

      Hi! I realized why it was not working for me at least. Here's what I was trying to do:

      #include <iostream>
      #include <cmath>
      
      int main()
      {
          // what I was doing
          std::cout << std::ceil(3/2) << std::endl; // c++ div is floor by default -> ceil(floor(x)) = floor(x) -> 1
          
          // what must be done
          std::cout << std::ceil(3/(2*1.0)) << std::endl; // make sure converted to float firstly -> 2
          return 0;
      }
      

      The above formula for ceil is a good workaround to the annoying mathematical detail.

      Cheers, foster

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

    add 0.5

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

    You can using a/b + (a%b !=0); !!!

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

still got no idea how to solve D

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

    In example 1, 1, 2, 2, so in this case, the mode will be 1, 1, 1, 2, right?

    Now, let's change our mindset. Let's consider if there are 5 numbers, from 1 to n (1, 2, 3, 4, 5). The mode can be 1, 2, 3, 4, or even 5. So we can construct 1, 1, 2, 2 to be 1, 3, 4, 2. The trick is to simply construct array b with all different numbers.

  • »
    »
    5 weeks ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    What I did is I first added all the unique elements in the order they appear. For example, if the input is 1, 1, 1, 2, 2, 5, 6, then I start by adding 1, 2, 5, 6 to my answer array. Then, I simply added a single occurrence of each number from 1 to n not in the given list of numbers until the output list is of length n. My submission is at 296645640

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

      Yes, I did the exact same thing! Just couldn't implement it correctly in time :')

»
5 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

extreme demon where

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

Lilypad

problem B is shit, please dont shit in the contest, i dont have a mirror and i didnt see any mirror or glass in my intire life , i dont know is reflection , i demand you to make the contest unrated , i also dont know what water is so i cant see my reflection , please just cancel the contest . YOU ARE VERY EVIL, i also opened codeforces mirror the text was not reversed , I WANT THE CONTEST TO BE CANCELED

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

    LOL you could have just write the string in a piece of paper and flip it and there was an option for unrated participation. skill issue

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

    Replace p with q and reverse it. It's not CP problem, it's rather a physics problem :)

»
5 weeks ago, # |
  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 :).

»
5 weeks ago, # |
  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.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Yes, I think DivinePunishment found that solution and it's a lot nicer.

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

      Hey macaquedev and SkibidiPilav I tried to implement G2 using the exact same approach but I need some help.

      Exact Approach:

      1. I did not remember how to find cycle in a functional graph so I found the inverse of the graph and use Kosaraju to find cycle

      2. I figured out that if I take each node in a cycle as a parent then the inverse graph will form a tree

      3. Now the problem becomes, given some trees, and each node has 1 value, find the total time it takes for each value to reach the root of the tree; each node pushes 1 value at each second(time unit) to its root

      4. The answer is just the maximum of (all the trees) +2

      I am getting WA using this approach. For the tree part, I counted the nodes in the subtree but doesn't seem to work. I have my code here(I hope its readable). I just wanted to ask if my approach is correct or reversing the edges(inverse) does not make a tree or anything that I might have missed?

      EDIT: Nevermind, I got it. But this was a very nice way to think about the problem.

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

    Yes! exactly what I did.

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

    Or you could start by processing nodes with an in-degree of zero and increment the values of their adjacent nodes by 1.

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

    I had the same approach !

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

g1 was nice

time was up while doing g2 :(

»
5 weeks ago, # |
  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

  • »
    »
    5 weeks ago, # ^ |
      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.

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

      How do I fix that?

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          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.

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

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

          • »
            »
            »
            »
            »
            »
            5 weeks ago, # ^ |
            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.

            • »
              »
              »
              »
              »
              »
              »
              5 weeks ago, # ^ |
              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.

              • »
                »
                »
                »
                »
                »
                »
                »
                5 weeks ago, # ^ |
                  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://codeforces.me/contest/2044/submission/296764832

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

                  Thanks a lot, understood

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

      can u also tell me whats wrong with submission

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          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.

»
5 weeks ago, # |
  Vote: I like it +7 Vote: I do not like it

Problem with the MODE was really interesting!

»
5 weeks ago, # |
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

»
5 weeks ago, # |
  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?

»
5 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

https://codeforces.me/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)

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

My best contest till date!! Thanks!!

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

why my submission 296750050 gives tle, i am not able to handle overflow may be can any one help in this.

  • »
    »
    5 weeks ago, # ^ |
    Rev. 4   Vote: I like it 0 Vote: I do not like it

    In your code:

    vector<int> g;
            int p=1;
            g.push_back(p);
            while(p<=r2){
                p *= k;
                g.push_back(p);
            }
     
            int ans=0;
            int q=g.size();
     
            cout<<q<<"\n";
            for(i=0; i<q; i++){
                cout<<g[i]<<" ";
            }
            cout<<"\n";
     
            for(i=1; i<q; i++){
                for(j=g[i]; j<=r2; j=j+g[i]){
                    int y=(j/g[i]);
                    if(y>=l1&&y<=r1&&j>=l2){
                        ans++;
                    }
                }
            }
    

    For this test case: 2 1 1000000000 1 1000000000 k = 2, g[0] = 2.

    Then you run this code:

    for(j=g[i]; j<=r2; j=j+g[i]) {...}
    
    

    g[i] = $$$2$$$ => Run from $$$2$$$ to $$$10^9$$$ with each step = $$$2$$$ => $$$5 * 10^8$$$ times
    g[i] = $$$4$$$ => Run from 4 to $$$10^9$$$ with each step = $$$4$$$ => $$$2.5 * 10^8$$$ times
    ...

    => TLE

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

      thank you. but i got it before looking in to your comment.

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

Loved the problems ! Solved upto C For the first time, almost got D aswell.

»
5 weeks ago, # |
  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

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

    that what I was trying to solve for first 40 mins of solving D lol

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

      same here.I was thinking how it can be problem D then I realised I read the statement wrong.

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

    Create a new vector (say D) of unique elements occurring in a, in the exact order as they occur. Now as you construct your vector b, maintain a set consisting of all modes of b upto the point it is constructed. Keep adding elements in b from D in a cyclic manner unless a[i] = some element already present in the set and set being its max size (in this case, add a[i] and empty the set).

    You can look at my submission here .

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

why not to use ceil function?

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

    I think it's because people do something like ceil(a/b) in C++ where a and b are integers, but forget that a/b will be a floor division by default (e.g. 5/2 gives 2), so ceil(a/b) will just be equal to a/b. If you want to use ceil, you can convert to a floating point data type, for example ceil((float) a/b). This is what I did in my submission for E 296692303.

  • »
    »
    5 weeks ago, # ^ |
      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
    
  • »
    »
    5 weeks ago, # ^ |
      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.

»
5 weeks ago, # |
  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?

  • »
    »
    5 weeks ago, # ^ |
      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

    • »
      »
      »
      5 weeks ago, # ^ |
        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

  • »
    »
    5 weeks ago, # ^ |
      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

    • »
      »
      »
      5 weeks ago, # ^ |
        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.

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

anyone use scc for G problem?

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

    yes, you can refer to my code.

»
5 weeks ago, # |
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****

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
      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

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

    Exactly!! The ai*bj thing annoyed

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

    ahh, now that you write it this way, it is clear that we can make it into the factorise form from here.

    What I missed is that I wrote it like this

    to_remove = sum of row i + sum of column j - ai * bj
    

    From my equation its not clear that we can factorise it.

»
5 weeks ago, # |
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
»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

i like problem D, very cool very sigma

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

can anyone point out why i got TLE for G1: https://codeforces.me/contest/2044/submission/296745366

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

nice contest, really liked problem E

»
5 weeks ago, # |
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.

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

why it is getting tle on test case 3: Problem F :

bool bs(vec arr , ll s ,ll t){
    ll i=0 ;
    ll j =s-1;
    while(i<=j){
        ll mid = (i+j)/2;
        if(arr[mid]==t){
            return 1;
        }
        else if(arr[mid]>t){
            j=mid-1;
        }
        else i=mid+1;
    }
    return 0;
}
ll fnd(ll c,ll d ,ll s1 ,ll s2 , ll n, ll m,vec m1,vec m2){
    ll v = c+s1;
    ll u = d+s2;
    ll s=0;
    if((bs(m1,n,v))&&bs(m2,m,u)){
        s++;
    }
    return s;
}
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    ll n,m,q;
    cin>>n>>m>>q;
    ll s1 =0; ll s2 =0;
    vec m1(n), m2(m);
    for(ll i=0;i<n;i++){
        cin>>m1[i];
        s1+=m1[i];
    }
    for(ll i=0;i<m;i++){
        cin>>m2[i];
        s2+=m2[i];
    }
    srt(m1); srt(m2);
    while(q--){
        ll x;
        cin>>x;
        ll s=0;
        ll f=0;
        if(x<0){
            f++;
            x=-1*x;
        }
        for(ll i=1;i*i<=x;i++){
            if(s!=0) break;
            if(x%i==0){
                if(f==0){
                    s += fnd(x/i,i,s1,s2,n,m,m1,m2);
                    s += fnd(-1*(x/i),-1*i,s1,s2,n,m,m1,m2);
                    s += fnd(-1*i,-1*(x/i),s1,s2,n,m,m1,m2);
                    s += fnd(i,x/i,s1,s2,n,m,m1,m2);
                }
                else{
                    s += fnd(x/i,-1*i,s1,s2,n,m,m1,m2);
                    s += fnd(-1*(x/i),i,s1,s2,n,m,m1,m2);
                    s += fnd(i,-1*(x/i),s1,s2,n,m,m1,m2);
                    s += fnd(-1*i,x/i,s1,s2,n,m,m1,m2);
                }
            }
        }
        if(x==0){
            ll v =s1;
            ll u =s2;
            if((bs(m1,n,v))&&bs(m2,m,u)){
                s++;
            }
        }
        if(s){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}
»
5 weeks ago, # |
  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!

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

CYAN, YAY!

»
5 weeks ago, # |
  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...????
  • »
    »
    5 weeks ago, # ^ |
      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.
»
5 weeks ago, # |
  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

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

problem.D shocked me orz

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

is it rated?

»
5 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

why did my solution to F get hacked ? (https://codeforces.me/contest/2044/submission/296732576)

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

Geometry dash????

»
5 weeks ago, # |
  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!

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

My soln for F is giving AC on GNU c++23 and tle on GNU c++ 17 :(

»
5 weeks ago, # |
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.

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

    Yep I upsolved following the editorial and it passed during the hacking phase. Later the system tests get updated with hacks that worked; sure enough, I got TLE

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    bruh

  • »
    »
    5 weeks ago, # ^ |
      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?

    • »
      »
      »
      5 weeks ago, # ^ |
      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.

  • »
    »
    5 weeks ago, # ^ |
      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!

    • »
      »
      »
      5 weeks ago, # ^ |
        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

      • »
        »
        »
        »
        5 weeks ago, # ^ |
        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

    • »
      »
      »
      5 weeks ago, # ^ |
      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://codeforces.me/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...

      • »
        »
        »
        »
        5 weeks ago, # ^ |
        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.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it +17 Vote: I do not like it

      Ok, I tried it and it looks possible: https://codeforces.me/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$$$.

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

        I made it: https://codeforces.me/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.

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

Reading the comments in Editorial post is more helpful than the post itself

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

Great editorial❤️

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

I couldn't understand the bs approach of problem E ,can anyone explain the approach in detail ?

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

    Ok let me explain it to you. i hope you understand that there will be at max 32 possible values of k we need to check for such that if there exists some x in interval l1 to r1 and y in interval l2 to r2 such y/x=ki (ki represents that possible value).

    NOW LETS UNDERSTAND BINARY SEARCH.

    let we considered ki from 32 possible values. now put a pointer l=l1 and r=r1. take mid point of these if mid*ki is between l2 to r2 then ok we got a possible x (in l1 to r1). for now put it in some variable (say left) and move your right pointer r to mid-1 to check if some other x smaller than left exists or not and keep searching till you get. else if mid*k1<l2 this means we need to check for higher values of mid which could be done by setting l=mid+1, else if mid*k>r2 we need to check for lower values which could be by setting r=mid-1.

    now using same logic find right (which is the right most value in l1 to r1 such that l2<= right*ki <=r2) now all values between left and right will be possible x for which some y exists in l2 and r2 such that y=x*ki.

    and now your final answer must be increased by ans+=(right-left+1). do it for all k's and cout<<ans<<endl... :). Upvote please.

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

Man! This was my first Codeforces contest, was waiting frantically for that lil bit of rating. Now I can just see this contest in my unrated list and not rated. PAIN!

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

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

»
5 weeks ago, # |
  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.

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

    It's the minimum distance of any node to a cycle, thus it will be the maximum distance overall. The wording is a bit weird but it should be correct.

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

E was very nice. I used floor instead of ceil fml...

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

    ~~~~~~~~~~~~~~~~~~~~~~ ll t=1; cin>>t; while(t--) { ll k; cin>>k; ll x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; ll maxi=max({x1,y1,x2,y2}); ll temp=1; vectordata; while(temp<=maxi) { data.push_back(temp); temp*=k; } data.push_back(temp); ll ans=0; for(ll i=0;i<data.size();i++) { ll lx1=x1*data[i]; ll ry1=y1*data[i]; if(lx1>=x2 && lx1<=y2) {

    ll temp=y1*data[i];
                ry1=min(y2,temp);
                ll num=(ry1-lx1)/data[i] +1;
    
                ans+=num;
            }
        }
        cout<<ans<<ln;
    }

    ~~~~~~~~~~~~~~~~~~~

    What is wrong in this ans I am getting WA on test Case 2

    • »
      »
      »
      5 weeks ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      hey, for your code once you try this test case

      2 1 7 8 15

      for k^n=2 w.r.t ur code "lx1" will be equal to 2.The if condition fails for this value but in the range from 2 to 14 u have 4 values which will give the ratio as 2 like 8/4,10/5,12/6,14/7 so u should not discard the entire range when lx1<=x2 there might be some overlapping numbers in the range . the answer for the given testcase is 7 i hope it helps

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

Could anyone help me with my code? https://codeforces.me/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.

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

This contest was my worst contest. Because who lives in Pakistan they know how many times in a day they have to face electricity issues.

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

When can we expect the rating changes for this contest?

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

This was my first contest, but why am i not getting rating?

»
5 weeks ago, # |
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.

»
5 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

can someone tell me why submission failed system tests (problem E)

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

G1 and G2 problems are amazing, learned something new. Please do continue to make such type of contests, like this one provide so much learning!!

»
5 weeks ago, # |
  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.

»
5 weeks ago, # |
  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;
}
  • »
    »
    5 weeks ago, # ^ |
    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$$$.

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

      What's interesting is that this code passed systest before the hacking phase. The hacking phase purged codes using std::map and those new tests got into systest. So this code, that had previously been accepted as upsolve, got retested and failed.

      I knew it would be better to store in a boolean vector, but I didn't feel like dealing with negative values. Soon enough I'll rewrite it though

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

      Just ended up adding the 10 characters "unordered_" in front of map and the solution passed. What a weird problem.

  • »
    »
    5 weeks ago, # ^ |
      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.

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

      Are you sure? I only insert if it contains the value in the loop

      for (int i = 0; i < n; i++) {
          cin >> a[i];
          ra[a[i]] = true;
      }
      
»
5 weeks ago, # |
  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?!

  • »
    »
    5 weeks ago, # ^ |
    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.

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hello,

Can somebody explain why this is TLE for Problem.

Submission: Submission

Thanks!

  • »
    »
    5 weeks ago, # ^ |
      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.

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

      But this Submission has worked with change in the logic, even though set() is used.

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

        Well, then it wasn't the case. I just think it is good to know that Python sets are hackable, and in some TLEs it is actually because of that.

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

I used a binary search approach for problem E. Still, it says, Wrong answer: 221st numbers differ—expected: '1', found: '0'. I can't see that test case, so please help me find it.

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

can anyone post any binary search solution for problem E

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

why don't way1 work, but way2 does?

void solve() {
     int k,l1,r1,l2,r2;
    cin>>k>>l1>>r1>>l2>>r2;
    int kn = 1;
    int ans = 0;

    for (int i = 0; kn <= 1e9; i++) {
        /* way1:-
            y / x = k^n
            x = y / k^n 

            l2 <= y <= r2 ------ (1)
            l1 <= x <= r1 
                => l1 <= y / k^n <= r1
                => l1 * k^n <= y <= r1 * k^n ------(2)
        */
        int intersection_l = max(l1 * kn, l2);
        int intersection_r = min(r1 * kn, r2);
        if(intersection_r >= intersection_l){
            ans += intersection_r - intersection_l + 1;
        }

 
        /* way2:-
            y / x = k^n
            y = x * k^n 

            l1 <= x <= r1 ------ (1)
            l2 <= y <= r2
                => l2 <= x * k^n <= r2
                => l2 / k^n <= x <= r2 / k^n 
                => ceil(l2 / k^n) <= x <= floor(r2 / k^n) ------(2)
        */
       
        // int intersection_l = max(l1, (l2 + kn - 1) / kn);
        // int intersection_r = min(r1, r2 / kn);
        // if(intersection_r >= intersection_l){
        //     ans += intersection_r - intersection_l + 1;
        // }

        kn = kn * k;
    }

    cout<<ans<<endl;
}
  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The main logic behind interval solution is this that- we find left_x by l2/k^n and right_x by r2/k^n and in this way we are sure that all x between left_x and right_x are acceptable and generate acceptable y, but in your way1 it is not necessary all y you counted between left_y and right_y is acceptable that is if it could be generated by some x such that x*k==y.

    thus counting acceptable x will generate the solution and not counting the y(which includes unacceptable values).

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

    In way1, your 2 equations still may result in some y that cannot be divisible by k^n, so you can't find integer x for these y.

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

https://codeforces.me/contest/2044/submission/296691884

I need help... when i run the test case on which this code fails in my vs code it is giving the answer as expected by jury. where is the mistake ? Is it on my side?

Please someone take a look at the submission.

  • »
    »
    5 weeks ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I run the failed test case on my local, and it is failed:

    I looked at your code: int (long long) overflow occurred in case: k = 10 ^ 9 and i = 30 ($$${10^9} ^ {30}$$$)

    I think you should not use pow built-in function because it return real number

    I updated a bit your code, and it passed:

        vector<ll> ks(1,1);
        // for (int i = 1; i < 32; i++)
        // {
        //     ks.push_back(pow(k, i));
        // }
        long long p = k;
        while(p <= (int) 1e9) {
            ks.push_back(p);
            p *= k;
        }
    
    
    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yeah I find the main mistake is overflow and not the logic. but in the final contest result it shows wrong answer that is why i got confused.

»
5 weeks ago, # |
  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
»
5 weeks ago, # |
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!

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

    O(N*M) would definitely TLE. Try to code a O(N+Q*sqrt(N)) solution.

    • »
      »
      »
      5 weeks ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      my implementation would roughly be O(N + Q*sqrt(X)*(log N))? this still doesnt pass :(

      how can i optimise it? Any help would be appreciated.. Thanks.

      296889036

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

        The absolute value of a and b is less than N, so You can use array to check the existence to remove the logN complexity from set or map.

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

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

»
5 weeks ago, # |
  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?

  • »
    »
    5 weeks ago, # ^ |
      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

»
5 weeks ago, # |
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?

  • »
    »
    5 weeks ago, # ^ |
      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.

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

      296887368 unordered_map (accepted)

      296887212 map (TLE at test 25)

      If you want you can check it out.

      And thanks, from next time I will be using 4 boolean vectors.

    • »
      »
      »
      5 weeks ago, # ^ |
      Rev. 2   Vote: I like it +6 Vote: I do not like it

      It's not always easy to hack unordered_maps. Check https://codeforces.me/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://codeforces.me/blog/entry/132929 for more details.

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

Can anyone give problem E binary search code?

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

    you can look at mine but it's ugly so I recommend sticking with the math formula because it's better

»
5 weeks ago, # |
  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 ?

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Can you tell why my code is giving TLE for question G1? I had though same approach

      Java 296825126

      C++ 296869151

      I know i am doing some useless work but still i guess the complexity is nearly O(n)

      PS-: Solved it 296926821

»
5 weeks ago, # |
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

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
        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

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          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.

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

Does a wrong submission decrease rating/ranking?

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

    During a div4 contest, each wrong submission on a problem you have successfully solved will increase your penalty time by 10 minutes

»
5 weeks ago, # |
  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

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

    The time limit on test cases is tight, so I think they wanted us to write a more efficient solution. I also got TLE at first, but then I noticed the constraint on a_i ranging from -n to +n. This allowed me to model my answer more effectively and avoid the TLE.

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

    Here, I explained it for you :) https://codeforces.me/blog/entry/137368

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

Incase someone wants to know the Binary Search solution for the Problem E

Solution

»
4 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hello, in problem E : solution 2 anyone knows why the left side of ⌈l2/k^n⌉ ≤ x ≤ ⌊r2/k^n⌋ is ceil ??

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

The TLE is the real demon in problem F.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it
Your code here...

import java.util.*;

public class contest2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt(); int[] a = new int[n]; long suma = 0; for(int i=0; i<n; i++){ a[i] = sc.nextInt(); suma+=a[i]; } int[] b = new int[m]; long sumb = 0; for(int i=0; i<n; i++){ b[i] = sc.nextInt(); sumb+=b[i]; } HashSet hs_col_row = new HashSet<>(); for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ hs_col_row.add((sumb-b[j])*(suma-a[i])); } } while(q>0){ long temp_sum = sc.nextLong(); if(hs_col_row.contains(temp_sum)){ System.out.println("YES"); } else System.out.println("NO"); q--; } } } ~~~~~

Why is this code giving time limit exceeded for problem F

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it
import java.util.*;

public class contest2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int q = sc.nextInt();
        int[] a = new int[n];
        long suma = 0;
        for(int i=0; i<n; i++){
            a[i] = sc.nextInt();
            suma+=a[i];
        }
        int[] b = new int[m];
        long sumb = 0;
        for(int i=0; i<n; i++){
            b[i] = sc.nextInt();
            sumb+=b[i];
        }
        HashSet<Long> hs_col_row = new HashSet<>();
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                hs_col_row.add((sumb-b[j])*(suma-a[i]));
            }
        } 
        while(q>0){
            long temp_sum = sc.nextLong();
            if(hs_col_row.contains(temp_sum)){
                System.out.println("YES");
            }
            else System.out.println("NO");
            q--;
        }
    }
}

Why is this giving time limit exceeded on problem F

»
4 weeks ago, # |
  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.

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

https://codeforces.me/contest/2044/submission/297166346

Is this inefficient? I keep getting TLE on either test case 3 or 4

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

The editorial for problem F is giving time limit exceeded in my case. Can someone please check it?

Edit: the solution got accepted for C++20, but not for C++17? can someone point me to how the language version changes can cause this issue?

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

Problem F: "Now, consider the effect of an operation on a column C. The beauty decreases by Ac*SumB".

When an operation is performed on a column C, shouldn't the beauty decrease by Bc * SumA? Please help.

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

Proof: My solution was flagged as cheated, but it uses well known concepts with published sources

Here's my blog post: https://codeforces.me/blog/entry/137548

I request MikeMirzayanov, cry, Proof_by_QED and Lilypad to kindly help.

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

I think G2 and G1 are just usually "Graph Theory" problems. They are not 1700 and 1900.

They are much easier.So Good Luck with solving them :3

»
3 weeks ago, # |
  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://codeforces.me/contest/2044/submission/296678418) should be banned. It's code I leaked and hacked. There are hundreds of people to ban easily.

»
6 days ago, # |
  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}$$$.