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

Автор cry, 3 дня назад, По-английски

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

2044A - Easy Problem

Problem Credits: cry
Analysis: macaquedev

Solution

2044B - Normal Problem

Problem Credits: Lilypad
Analysis: Lilypad, macaquedev

Solution

2044C - Hard Problem

Problem Credits: cry
Analysis: macaquedev

Solution

2044D - Harder Problem

Problem Credits: cry, vgoofficial
Analysis: macaquedev

Solution

2044E - Insane Problem

Problem Credits: vgoofficial,Lilypad
Analysis: macaquedev, chromate00

Solution 1 (Binary Search)
Solution 2 (Intervals)

2044F - Easy Demon Problem

Problem Credits: chromate00, vgoofficial
Analysis: SkibidiPilav

Are you a python user and failing test 12?
Solution

2044G1 - Medium Demon Problem (easy version)

Problem Credits: Lilypad
Analysis: macaquedev

Solution

2044G2 - Medium Demon Problem (hard version)

Problem Credits: Lilypad
Analysis: vgoofficial

Solution

2044H - Hard Demon Problem

Problem Credits: cry
Analysis: chromate00

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

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

first ez

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

    when the rating will publish

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

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

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

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

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

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

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

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

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

              just wait you well get it normally.

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

          Same bro I also didn't get any rating

»
3 дня назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится
  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +4 Проголосовать: не нравится

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

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

    nice sol and walkthrough, thanks!

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

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

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

      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;
          
      
      • »
        »
        »
        »
        35 часов назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        yeah basically

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

          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

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

            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

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

My Text Editorial (A-D)

Screencast

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

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

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.

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

    cope and seethe

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

      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.

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

    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.

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

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

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

        Indeed, I am.

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

        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?

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

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

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

boy do i love getting TLE'd

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

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.

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

omg my editorial for F made it uwu

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

Very creative problem names. (like my variable names)

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

Thanks for the awesome problem set -_-

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

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

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

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

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

https://mirror.codeforces.com/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?

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

Thanks for the great contest!

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

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

still got no idea how to solve D

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

    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.

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

    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

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

extreme demon where

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

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

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

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 :).

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

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.

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

g1 was nice

time was up while doing g2 :(

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

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

My code

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

    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.

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

      How do I fix that?

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

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

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

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

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

            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.

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

              Doesn't work!

              Ok I will just ask ChatGPT then.

              Edit: That was no help.

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

                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

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

      can u also tell me whats wrong with submission

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

        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.

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

Problem with the MODE was really interesting!

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

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

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

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

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

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)

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

My best contest till date!! Thanks!!

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

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

  • »
    »
    39 часов назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    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

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

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

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

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

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

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

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

    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 .

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

why not to use ceil function?

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

    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.

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

    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
    
  • »
    »
    3 дня назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

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

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

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?

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

    "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

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

      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

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

    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

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

      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.

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

anyone use scc for G problem?

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

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****

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

    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.

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

      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

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

    Exactly!! The ai*bj thing annoyed

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

    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.

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

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

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

i like problem D, very cool very sigma

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

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

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

nice contest, really liked problem E

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

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.

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

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;
}
»
2 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

CYAN, YAY!

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

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...????
  • »
    »
    2 дня назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.
»
2 дня назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

problem.D shocked me orz

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

is it rated?

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

Geometry dash????

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

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

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

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

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

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.

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

    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

  • »
    »
    41 час назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    bruh

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

    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?

    • »
      »
      »
      38 часов назад, # ^ |
      Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

      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.

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

    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!

    • »
      »
      »
      36 часов назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      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

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

        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

    • »
      »
      »
      30 часов назад, # ^ |
      Rev. 2   Проголосовать: нравится +4 Проголосовать: не нравится

      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...

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

        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.

    • »
      »
      »
      27 часов назад, # ^ |
        Проголосовать: нравится +14 Проголосовать: не нравится

      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$$$.

      • »
        »
        »
        »
        26 часов назад, # ^ |
        Rev. 5   Проголосовать: нравится +14 Проголосовать: не нравится

        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.

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

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

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

Great editorial❤️

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

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

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

    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.

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

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!

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

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

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

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.

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

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

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

    ~~~~~~~~~~~~~~~~~~~~~~ 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

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

      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

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

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.

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

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.

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

When can we expect the rating changes for this contest?

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

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

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

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

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

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

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

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!!

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

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.

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

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;
}
  • »
    »
    39 часов назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    $$$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$$$.

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

      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

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

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

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

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

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

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?!

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

    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.

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

Hello,

Can somebody explain why this is TLE for Problem.

Submission: Submission

Thanks!

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

    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.

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

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

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

        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.

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

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
»
41 час назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

can anyone post any binary search solution for problem E

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

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;
}
  • »
    »
    39 часов назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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).

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

    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.

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

https://mirror.codeforces.com/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.

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

    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;
        }
    
    
    • »
      »
      »
      37 часов назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.

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

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

Java Code G1
Converted C++ code G1
»
38 часов назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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!

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

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

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

      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

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

        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.

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

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

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

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?

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

    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

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

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 :) SkibidiPilav

chromate00

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

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

    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.

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

Can anyone give problem E binary search code?

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

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

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

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

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

    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.

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

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

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

    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.

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

      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

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

        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.

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

Does a wrong submission decrease rating/ranking?

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

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

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

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

submission

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

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

Solution

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

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

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

The TLE is the real demon in problem F.