This blog will describe an interesting trick I've seen in a few tasks.
This is my first time writing a tutorial blog so sorry if it makes no sense.
$$$2$$$ and $$$3$$$ can build anything you want.
This trick is based on this observation: any number $$$x \gt 1$$$ can be represented as the sum of $$$2$$$'s and $$$3$$$'s.
How do we use this??????
We can use this idea if we take something of some length from somewhere. Often, we can't just look at all the things we can take because it could be too slow. Instead, what if we limit ourselves to taking things of length $$$2$$$ or $$$3$$$?
I know this is very abstract and high-level, but these tasks will show what I mean.
1624E - Masha-forgetful
This is just a very direct application of this trick. Instead of worrying about every substring, we can only account for substrings of length $$$2$$$ or $$$3$$$, since any substring longer than that can be broken down into shorter substrings. So any solution generated with lengths $$$ \gt 3$$$ can also be generated this way. Submission: 313317958
1616D - Keep the Average High
Funnily enough, this problem was released less than 2 weeks before 1624E.
Firstly, let's subtract $$$x$$$ from all the numbers in the array. Now, the condition becomes $$$a_l + a_{l+1} + ... + a_r \ge 0$$$. Now, here's the genius part. Instead of worrying about every subarray, is it possible just to consider subarrays of length $$$2$$$ and $$$3$$$? It turns out, yes. If every subarray of length $$$2$$$ and every subarray of length $$$3$$$ have a non-negative sum, then every subarray has a non-negative sum. (Except for subarrays with one element).
From here, we can do a simple DP: $$$dp(i, j, k)$$$ is the maximum number of elements we can take from the first $$$i$$$ elements, $$$j$$$ tells us if we have taken $$$a_i$$$, and $$$k$$$ tells us if we have taken $$$a_{i-1}$$$. Submission: 316394601
Conclusion
If there are other tasks, please post them in the comments. My dream would that this trick gets added to USACO Guide.








Bezout's theorem
Bezout's identity is for all integers, but here only nonnegative integers are allowed.
Chicken McNugget Theorem
Exactly.
This can be used to solve PizzaForces in a fun way.
Any integer N > 1 can be represented as N = 2*a + 3*b, where a >= 0 and b >= 0. Extending further:
=> N = 2*(a-x) + 3(b-x) + 2*x + 3*x
=> N = 2*p + 3*q + 5*r
If we write 2*p as 4*l, the only number that we lose is N = 2. That is, any number N > 2 is also of the form N = 3*q + 4*l + 5*r.
Which can be used to solve the above question.
Cool blog, although I think this is not a "trick", more just something you see in like five or six problems and then internalize.
In fact, I think this can be generalized, and is kind of part of a much larger class of techniques, where you "reduce the search space", one example of this is when, say you're performing certain moves on the coordinate plane which have an additive structure (commutative and all that jazz), and you want to be able to reach all possible coordinates. In this case, you can just check whether it is possible to reach $$$(1, 0)$$$ and $$$(0, 1)$$$, since $$$(x, y) = x(1, 0) + y(0, 1)$$$ (we can just perform the moves leading us to $$$(1, 0)$$$ $$$x$$$ times, and the moves leading us to $$$(0, 1)$$$ $$$y$$$ times). Another example is when, say you want to be able to reach all possible XOR values $$$\le s$$$ as a subsequence XOR of a certain array. Then, by a very similar argument, you can just check that all powers of two $$$\le s$$$ are achievable, since any other number is representable as a sum of them anyway. Yet another example is when you reduce "flip $$$b_1$$$ or $$$b_2$$$ or $$$b_3$$$ or $$$\dots$$$ or $$$b_n$$$ consecutive characters in a binary string" to "flip $$$g = \gcd(b_1, b_2, \dots, b_n)$$$ consecutive characters in a binary string" (because Bezout).
Some Dirichlet convolutions can be computed faster (see powerful number sieve) because of the fact that every powerful number (that is, whose every prime factor has degree at least $$$2$$$) can be represented by $$$a^2b^3$$$ (because each degree at least $$$2$$$ is a sum of some twos and threes).
More problems: P12013 (but it's only available in Chinese)
This problem is mostly based on this trick. RACMP
problem based on non-negative Diophantine Equation 311C - Fetch the Treasure
Auto comment: topic has been updated by Sul_A. (previous revision, new revision, compare).
This helped so much thanks man a true G.O.A.T