@dupanosi
iAccount based inGreece
About this account
- Account based in
- Greece
- Connected via
- Greece App Store
Account-level information from X, not a live location or the device used for a specific post.
19. co-founder of https://nitter.cf/t.co/RL41sdL0xD & https://nitter.cf/t.co/DSCDfZsKeq blessed since birth.
Joined August 2022
- Tweets12.1K
- Following1.7K
- Followers1.5K
- Likes15.5K
dusan retweeted
Day 30 of 30!
One leetcode problem a day or I pay a follower $500. No AI. No autocomplete. No copilot
This may be the hardest problem I solved in my career
Tomorrow will be the final post for this challenge
Problem of the day : 3655. XOR After Range Multiplication Queries II
Same problem as yesterday but 100x the constraints. The parameter that makes it slow is the parameter that makes it fast.
k is the stride. A large k means a short progression, so walking it directly is cheap. A small k means a long progression, but small k also means there are fewer than sqrt(n) possible values of k, so every query sharing a stride can be settled in one sweep.
Balancing at sqrt(n) is not a heuristic. It is where the two costs cross. At n = q = 100,000 both sides land near 3 * 10^7. Measured worst case: 70 ms, with the two halves contributing roughly equally.
Two bugs in here are quiet and both cost me time:
→ the closing marker goes at last+k, not r+1. r+1 usually sits in a different residue class, so cancelling there corrupts a chain the query never touched, and the wrong answer shows up nowhere near the query that caused it.
→ last is l plus ((r-l)/k)*k, not r, because the progression normally stops short of r.
Neither shows up on k = 1 tests. That is exactly what makes them dangerous.
*code in the first comment
Day 29 of 30
One leetcode problem a day or I pay a follower $500. No AI. No autocomplete. No copilot
Problem of the day : 3653. XOR After Range Multiplication Queries I
My first question was not how to make it faster. It was whether faster can exist.
The task: apply a bunch of strided range multiplications mod p, then XOR the whole array. Range multiply screams lazy propagation or a difference array. Defer the updates, materialise once.
It cannot work here. The output is a XOR, and XOR does not interact with multiplication mod p in any structured way. You cannot combine two elements without knowing both exact values, and you cannot get a bit of a product from bits of its factors. The final array has to exist in full before anything can be read off it.
That kills deferral at the root and leaves one question: is the direct walk affordable? Worst case is a million modular multiplications. 667 microseconds. Done.
The actual bug available is arithmetic, not algorithmic. Skip the mod inside the loop and from a starting value near 10^9, the second multiply is already 10^19. 64 bits gone, silently, no error.
→ check whether the fast thing can exist before you build it
→ sometimes the loop in the statement is the solution, not a placeholder for one