AzizbekSabirov's blog

By AzizbekSabirov, history, 43 minutes ago, In English

Hello Codeforces!

I’ve been getting a lot of TLEs recently on problems where my core logic and Big-O complexity seem completely fine. Since I code in C++, I know it's usually just some hidden overhead in my implementation.

I already use fast I/O, replace endl with \n, and always make sure to pass my vectors by reference.

What are your favorite small, everyday optimization tricks? Are there any common STL traps, surprisingly heavy operations (like modulo or maps), or simple habits I should adopt to shave off those extra milliseconds?

I'd love to hear your quick tips. Thanks in advance! If this possible things like makes vector faster or like anyhting small just let me know please!

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
32 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

it is absolute AI dont read it sh111t

  • »
    »
    32 minutes ago, hide # ^ |
     
    Vote: I like it +2 Vote: I do not like it

    sybau

»
31 minute(s) ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Pragmas for Auto-Vectorization

If your code relies on heavy loops over primitive arrays or vectors, adding compiler pragmas at the top of your file enables SIMD instruction set auto-vectorization (AVX2/BMI2)

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

Modulo Division Overhead

The Problem: Modulo (%) and division (/) operations require hardware-level division instructions that take ~20–40 CPU cycles, whereas addition and subtraction take only 1 cycle. In loops running $$$10^8$$$ times, raw modulo will TLE.

The Fix: Replace modulo addition with conditional subtraction

// Slow:
a = (a + b) % MOD;

// Fast:
a += b;
if (a >= MOD) a -= MOD;
  • »
    »
    12 minutes ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Second suggestion is a great way to get a lot of WA3s, it will break or at least be hackable if you forget modulo anywhere, add more than 1 variable at once, or do multiplications or subtraction under modulo

    If you really need to optimize modulo use Barret reduction, it fails with results greater than or equal to $$$\text{MOD}^2$$$ but that's a lot better than $$$2\text{MOD}$$$