Sul_A.'s blog

By Sul_A., history, 17 months ago, In English

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.

Proof
Alternate Proof

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).

Proof

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.

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

| Write comment?
»
17 months ago, hide # |
 
Vote: I like it -7 Vote: I do not like it

Bezout's theorem

»
17 months ago, hide # |
Rev. 2  
Vote: I like it +5 Vote: I do not like it

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.

»
17 months ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

Cool blog, although I think this is not a "trick", more just something you see in like five or six problems and then internalize.

yap
»
17 months ago, hide # |
 
Vote: I like it +50 Vote: I do not like it

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).

»
17 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

More problems: P12013 (but it's only available in Chinese)

»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

This problem is mostly based on this trick. RACMP

»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

problem based on non-negative Diophantine Equation 311C - Fetch the Treasure

»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
17 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

This helped so much thanks man a true G.O.A.T