Блог пользователя Ahmed-Nawaz

Автор Ahmed-Nawaz, история, 5 месяцев назад, По-английски

can anybody who uses #define int long long or #define int int64_t or doesnt uses int and only uses 64 bit integers please tell if through out the career due to MLE/TLE they have ever needed to switch to 32 bit and if it made a difference in verdict or not?

  • Проголосовать: нравится
  • +17
  • Проголосовать: не нравится

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

no da den

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I have had to switch to 32bit int only once or twice (never on codeforces). One of the times it was for some cses dp problem with very tight limits

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

I have done problems where using long long everywhere will lead to TLE (specifically: https://usaco.org/index.php?page=viewproblem2&cpid=1207)

Just use 32bit numbers unless the problem statement shows you that values would be large or you are computing products of large numbers.

»
5 месяцев назад, скрыть # |
← Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

MLE is situational, just check if (array size) * 8 bytes instead of 4 comfortably fits in the limit. Most problems do not care too much about memory.

For time, I ran some benchmarks for you. This is on GCC with -O2 on my i7-10700k.

  • 300M Adds: 0.42s vs 0.42s
  • 300M Multiplys (actually multiply-then-increment): 0.61-0.63s vs 0.61-0.62s
  • 300M Divides: 0.42-0.44s vs 0.42-0.44s (yes, divides are supposed to be slower, but the loop I have testing them is not the greatest)

Basically there's no difference in 32 vs 64 bits because they all probably execute exactly the same in the ALU.

(Bad) code: https://github.com/greatericontop/CompetitiveProgrammingStash/blob/main/random/benchmark.cpp

I tested again with a small loop written fully in assembly, 1 billion additions run in 0.20-0.21s vs 0.20-0.21s again.

Code: https://github.com/greatericontop/CompetitiveProgrammingStash/blob/main/random/benchmark_assembly.s (revised version to reduce the effect of pipelining)

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I have encountered a similar situation before. There was a problem where I use long long in a dp array that did not require long long, resulting in TLE. After changing it to int, it passed.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

i solved one questions i dont really remember the rating and problem statment ,as much i remember it was 1400 rated problem where i came up with almost 1e7 time compexity solution ,it was getting TLE repeatedly and as mush as i can think it was a solution which cant be optimized further , i just thought why not switch from long long to int and surprisingly it passed the all test cases , from that day i get so scared to use long long . if i find the problem , i will update.

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

I mostly use int64_t in my codes. TLE/MLE often occur in problems with strict constraints. Sometimes I put #define int64_t int32_t at the start of my codes.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

I use #define int long long in most of my code, and it's really effective as I rarely see Wrong Answers due to overflow. However, it's not harmful; if possible, I still recommend thinking carefully before setting a variable to int64 or int32, as they have their own advantages.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Happened to me couple of times with some DP problems , got MLE using ll = long long; then changed it to int and passed peacefully.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

many times, especially in Macedonian national contests where the TL and ML limits are tight due to limitations of the judging system