BerryYoghurt's blog

By BerryYoghurt, history, 5 years ago, In English

Hello. I was solving 1399E1 - Weights Division (easy version), and my initial solution had a TLE on test case 5. I checked out the tutorial and even read the attached solution, but I could not for the life of me find the reason why it exceeds the time limit. I have submitted 6 versions, each one with a slight optimization over the previous, until I finally figured out that an overflow is causing an infinite loop.

Now the question: Here is a version of the code which I thought should work 127717319, and here is the version that actually worked 127717507. The edges matrix stores for each edge the weight at index 0 and the number of paths this edge contributes to at index 1. It overflowed when it was a matrix of ints, and worked when I made it long long (this is the only difference between the two versions). Could anybody figure out why it overflows if the weights do not exceed 10^6; that is an int overflow should NOT happen?

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it

By BerryYoghurt, history, 6 years ago, In English

Hello, I've recently solved this problem and here is my solution. (Takes 1575 ms in the worst case) Since I am new to cp I decided to look around in the other submissions to see how different the execution times are and whether there are more efficient algorithms. I found many submissions which solve the problem in under 300 ms, but one was particularly surprising, namely because the algorithm is almost identical to mine!

I can, however, spot three main differences. First, the other solution uses int for all the values, whereas mine uses long or long long depending on the range. I tried changing all to int in my code but as I had expected it gave wrong answers for large numbers.. I can't understand why it works in the other solution.. Second, the other solution uses C i/o (i.e. printf and scanf) whereas I use cin and cout. Third, the other solution doesn't initialise any variables, but I assume iirc they're automatically initialised? Either way, for the sake of correctness, they have to be initialised, so this point shouldn't cause the time difference.

So my question is, to sum up: what is the quintessential difference that causes this colossal difference? Surely "different CPUs" should be out of question since it's all Codeforces servers, no?

Full text and comments »

  • Vote: I like it
  • +7
  • Vote: I do not like it