neckbosov's blog

By neckbosov, history, 5 weeks ago, In English,

984A - Game

tutorial

984B - Minesweeper

tutorial

983A - Finite or not?

tutorial

983B - XOR-pyramid

tutorial

983C - Elevator

tutorial

983D - Arkady and Rectangles

tutorial

983E - NN country

tutorial
 
 
 
 
  • Vote: I like it  
  • +71
  • Vote: I do not like it  

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

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

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

Can't access codes. Thanks for the editorial though !

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

Your code(38299456) for problem Div2. C / Div1. A is not accessible. Please fix it.

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

    Update: Actually none of the codes in the editorial is accessible.

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

The codes are accessible. Click on the "tutorial" link to reveal a "solution" link towards the end.

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

    They were not accessible at the time of our comments :)

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

Can you prove that your optimization in A improves complexity? Or have you just put there some random opt and noticed that it significantly sped up on tests you prepared? The way it is put right now is for me kinda unserious.

Look here for relevant discussion: http://codeforces.com/blog/entry/59457?#comment-431043

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

Can anyone explain div2 C more clearly?

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

I think problem div 2 C the whole round and i can't figured it out till i read the tutorial and know that a problem is finite only if exist k that q | p*b^k. But how do we figure out this fact?If you know this fact,then the problem will be easily solved.

Sorry for poor English.

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

    Try dividing a smaller number with a larger one in some base b . You will find that at each step of division we will be doing r=(r*b)%q . After some point of time if r=0 then we are done .In simple words if (p*b^k)%q=0 for some k then fraction will be finite .

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

    I got it!! :D

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

    My thought process for base 10:

    Let's assume our fraction is . If q's prime factors are 2 and 5, we can always multiply it by 2 and 5 until it becomes a power of 10, i.e. q2a5b = 10n. Let's assume q has some other prime factor, e.g. 3. Then, there must exist such that q2a5b = 10n , which is obviously impossible, since the right-hand side must be divisible by 3 too. It is easy to generalize this to a base with arbitrary prime factors.

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

For Div 1.C, the state can be defined as: the number of employees that have already entered to the elevator (m), the destinations of the employees that are currently in the elevator (x1, x2, x3, x4) (xi may be zero, which means the corresponding position is empty), and the floor where the elevator currently is (f). However, there are 2000 × 104 × 9 ≈ 2e8 different states; to reduce the number of states, we may ''canonicalize'' destinations of the employees that are currently in the elevator, by sorting x1, x2, x3, x4 to make sure that they are in ascending order. Now the number of states is only about 1e7, which is small enough. Now we can directly perform BFS with memoization. For each state (m, x1, x2, x3, x4, f), we can

  • Go up one floor, if f < 9;
  • Go down one floor, if f > 1;
  • Let one employee get out of the elevator, if xi = f;
  • Let the next employee get in the elevator, if x1 = 0 && m < n && am + 1 = f;

Each of these operations takes exactly one second, so we do not need Dijkstra or other shortest path algorithm; BFS works here. Note that, we ''violate the atomicity'' of the second command described in the problem; however, it is not hard to prove that such violation won't imporove the final answer.

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

am i the only one solved div 1 C using BFS?

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

I didn't understand Div. 2 D / Div. 1 B. After reading the problem, I thought that the function given just finds the XOR of all elements present in the input sequence and given l and r, we need to find the maximum value of XOR from all continuous sub-sequences. But, I got stuck in sample test case 2 (query 1).

6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2

Here the interval is [1, 6]. Shouldn't the maximum value of the function be 63 (XOR of all elements A[1]-A[6]) instead pf 60? I didn't understand the Editorial solution. Please tell me if I am making any mistakes in understanding the problem statement!

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

    f(1,2,4,8,16,32) -> f(1^2,2^4,4^8,8^16,16^32) = f(3,6,12,24,48) -> f(3^6,6^12,12^24,24^48) = f(5,10,20,40) -> f(5^10,10^20,20^40) = f(15,30,60) -> f(15^30,30^60) = f(17,34) -> f(17^34) = f(51) = 51

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

      Oh no! I understand now. This problem is not that simple as it looks. I made a terrible misunderstanding! :-(

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

In div2 B in some test cases input says 100 n and 100 m but the input given was 5 n and varying m . My code is giving wrong answers on those types of cases. Please look upon it.

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

detailed explaination about div2 C. my proof to div 2.C(div 1. A)

UPD: Thanks to I_LOVE_SMRITI_KIRAN and thanglong, they find some unclear sentences and welcome you to give me more so that i can do it better. Here is what i rewrite.

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

    Thanks buddy

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

    Can you explain the solution for this testcase when p=7 q=5 base = 7 ?. p in base b can be written as 10 and 5 can be written as 5. so p/q= 10/5 and the answer becomes FINITE. But the actual answer is INFINITE .. Any help where am i going wrong?

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

      init: p=7,q=5,base=7;

      1. the first opt: reduction of a fraction
      gcd(p,q)=1;
      p=p/gcd(p,q)=7;
      q=q/gcd(p,q)=5;
      
      1. the second opt: division until gcd(q,base)==1
      x=gcd(q,base)=1;
      while(x!=1){//now x==1,break;
          blahblah;
      }
      if(q==1){//now q==7,False, turn to the else opt
      
      }else{
         printf("Infinite\n");
      }
      

      My explaination use "1/p" to explain how to judge not using "1/q", which maybe puzzle you, i should apologize for that if so.

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

    Why did you have to make p,q relatively prime?

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

      e.g. p=3, q=30, base=10, now 3/30 is just 1/10=0.01, is finite, however if you do not do reduction of a fraction,you will got the wrong answer infinite.

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

    @zhsq11 You have a very beautiful handwriting. ^_^

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

      Thank you for your praise. ^_^ It is the first time that a native English speaker praises my handwriting. :)

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

        One last question,can you prove that p*base^n>q?,thanks.

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

          emm...i don't think it is must to make p*base^n>q.

          For a fraction p/q, if we prove 1/q is finite, then p/q must be finite. so after reduction of the fraction, it will be not relevant with the numerator p.

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

        Yeah! I really liked it :) but English is my 2 language :P

        Also can you please help me debug my solution to problem C (Div 2) ?

        Code link

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

          I don't understand the meaning of the following code.

          ll temp=b;
          while(temp%c==0){
              temp=temp/c;
          }
          if(temp==0||gcd(temp,c)==temp)
              flag=1;
          

          In fact, it's not a must to judge which one is bigger in b and c in your code. what we need to do is only to find whether b has a factor that c doesn't have.

          here is my submission

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

            Actually, I am saving b's value inside a temporary variable 'temp' and then dividing it with base c till I can. Then I check whether temp can be reduced to contain only the factors with base. I understand temp ==0 part and the comparison is unnecessary.

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

              I got the mistake.

              This is the case where my code would break:

              1 1 54 9

              Finally AC after 13 submissions and 5 hours.

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

    also in the last paragraph, I think you mean "(base)^n can be divided by q". Nice proof btw...

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

    Thank you

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

    Thank you :D

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

For div 2 C, after dividing q by gcd(p,q), I calculate (b^(10e18))%q. The answer is finite if the result is 0 else infinite. Since q<10e18, the exponent of any of its prime factor is less than 64. So b^(10e18) should always be divisible by q if all prime factors of q are factors of b as well. Can anyone explain why this gives a wrong answer on test case 11?

Code: http://codeforces.com/contest/984/submission/38308682

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

    Overflow in exponentiation function at line x = (x * x) % MOD .

    Since x can be as large as 1018. A prerequisite for the exponentiation is that the square must not overflow.

    Your argument however seems correct.

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

I didn't like the way the last paragraph of 1E was written (took quite a bit to figure out), so for anyone who might be struggling to understand it, I hope this helps:

We have reduced the question to the following cleaner problem:

Given some paths in a tree, determine if there is a subpath between s_i and t_i. (2e5 queries on the same set of paths). If one is an ancestor of the other, we can possibly handle it together with the earlier binary lifting. Otherwise, this is equivalent to asking if there exists a path with endpoints in the subtrees of s_i and t_i.

Using the Euler's Tour technique, a subtree gets transformed into a range. So now each path can be represented by a start and an end point, and we want to know if there is some path which starts in the range corresponding to the subtree of s_i and ends in the range corresponding to the subtree of t_i (wlog assume s_i comes before t_i in the tour).

If we represent a path as a pair (a, b) -- the start and end points, we realise that this is an offline 2D range query. We now solve the black-boxed problem.

If we had a good bookcode for 2D segment tree we were confident in, we could stop here now, but we can do better since it is offline:

We will be using a 1D fenwick tree to count the number of points with x-coordinates in a certain range. Furthermore, for a rectangle, we will be making 2 queries: the number of points under the top of the rectangle and the number of points under the bottom of the rectangle (and we can then subtract).

We process points and queries simultaneously in increasing y-coordinate. Hence, when a query is processed, the fenwick tree on the x-coordinate reflects all points under the current y-coordinate which gives us what we require.

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

    Thanks for writing up a much better explanation!

    I couldn't understand the last paragraph of the tutorial solution to 1E at all, even after rereading it dozens of times.

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

Did anyone do recursive dp solution for Div2 D / Div1B ?

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

"It's neccesary to do some optimizations to pass TL."

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

For Div1 A, I couldn't come up with the iterative reducing technique of Q until it reaches 1. Nice technique, by the way. To my surprise, java BigInteger worked within TL.

BigInteger Solution

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

Can anybody explain me please at div1 B, why f( l , r ) = f( l , r-1 ) ^ f( l+1 , r) ?

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

    Look at the "pyramid" for f(1, 2, 4, 8), the arrows represent XOR. (Sorry for the poor drawing):

    And the pyramid for f(1, 2, 4). Can you find it in f(1, 2, 4, 8)?

    This is the basic idea. I think you can prove it by induction on N := number of parameters in f.

    For N = 2, it is obviously true, and then...

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

Div1C. Any relation with editorial and solution code? The definition of 'state' is different at least i think. The solution code is just representing the state in base of 10.

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

Can anybody point me to the proof of the maths applied in Div2 C problem

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

How to observe "dp[i][j] = dp[i — 1][j + 1] ^ dp[i — 1][j" in Div2 D or Div1 ?

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

    draw it!!

    or by experience ( and guessing XDD

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

Can someone give detailed explanation of Div 2 D. How the array dp[n][n] is formed?

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

In div1B , f(l,r) seems to be f(l,r-1) ^ f(l+1,r) , any intuitive or easy proof of this?

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

    Induction.
    f(a(l),..,a(r)) = f(a(l)^a(l+1),.....,a(r-1)^a(r)) =
    f(a(l)^a(l+1),....,a(r-2)^a(r-1)) ^ f(a(l+1)^a(l+2),.....,a(r-1)^a(r)) (induction step on smaller length) =
    f(a(l),...,a(r-1)) ^ f(a(l+1),....,a(r)).

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

      can you please explain in more detail

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

        please see this link

        this 1

        this 2

        this

        codeforces.com/contest/984/submission/38316222

        and still you have question you can ask!

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

          How do u guarantee that a[i+1][j] is already calculated when u calculate a[i][j] in step 2?

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

        First equality is coming from the definition of f.
        Second equality is coming from the induction. (Assuming that our assumption holds for smaller arrays. As a base step of the induction, length of the array is 2.)
        Third equality is coming from the definition of f.

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

      Actually, after knowing the answer it seems trivial. But initially, how you came up with such recurrence? Is it standard, or just by observation?

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

My code for div2C (div1A) is failing on the 7 case. Can someone please help me debug it?

Thanks in advance.

Code link

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

    line 31 else if (b>c)

    you didn't handle the case b<c

    like b=20 and c=25

    that is finite

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

can anybody please clarify the statement written for 483(div 2)D ..... i m really confused....

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

good problems in this problemset.

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

Sorry, I am a newbie and I am confused in the mathematical notation | in (q ∣ p⋅b^k). Can anyone please explain?

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

Anyone solved D with dp + segment tree? I observed how to calculate f values, but to calculate max value in range I used segment tree.

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

    Did your solution work? DP array takes 5000x5000 memory which is okay, but segment tree on 2-D array takes 16x5000x5000 space which is too much. Even if we get the memory, build_tree would time out for such a huge tree.

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

      Its not 2d segment tree. You can see solution. I insert dp values in specific order in 1d array and then 1d segment tree on that array.

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

Thank you for the contest. However, I think the time limit for problem C div 2 is too tight. I tried your solution without fast input/output. It still got TLE. Here is my submission: http://codeforces.com/contest/983/submission/38354529.

I think it would be better if you consider to extend the time limit a little bit more next time.

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

    Taking into consideration when fast input is needed is part of solving problems, it's the same as considering when long long is needed or not.

    I agree the TL was a bit tight but the setters did it intentionally. At least they didn't hide it and put the worst case for the "naive" algorithm in pretests.

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

      I think If needed it should be mentioned in problem statement. Or if the time limit difference is so crucial then I think there probably be another type of input like randomly generating input based on a fomular. But yea, I will consider fast input in my solution next time. Thank you.

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

...

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

For Div 2 E, could someone explain how state is stored? I want to use an integer, so that the ith digit (i from 1 to 4) is the floor that the ith person wants to go. The 5th digit is the current floor the elevator is on. But there must be a better way.

Also, "The fast one is we say we go from the floor a[i] to the floor a[i + 1] and iterate over the persons who we let come in on the way." What does this mean?

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

What is the state of dp in DIV.2 D? Thanks in advance :-)

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

    The left index and the right index of your segment

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

For the div1 D, how can I figure out this fantastic segment tree. it's to difficult for me to choose the three features to describe this problem... Please help me understand this algorithm further.. THX!

»
13 days ago, # |
  Vote: I like it +1 Vote: I do not like it

used priority queue but not working properly please help https://ideone.com/JE16M9

»
12 days ago, # |
  Vote: I like it 0 Vote: I do not like it

HELP HELP HELP !!! In div2 C/div1 A If i use " ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); " then it is not showing TLE else it is showing TLE on testcase 11 ? can someone explain why ?

And tell me how can i know where to use this and where not to use this ?