Given integers and (with ) we say that another integer is nice if and is an integer between and .
We are given and . Find the sum of all nice integers. (If the answer is too large, print it modulo .)
(Please see the problem statement for further clarification)
- Math, Formula
- Divisibility, Quotient, Remainders
- "Write out" the formula
- and are medium sized
- Brute force, somehow
- Summing, counting
Review
Summary
Upon looking at the problem, we notice that it will likely be a math problem. Because the numbers (, , and the total sum / answer) can be fairly large, we might expect that there will be some kind of nice formula that we will have to compute. However, because and can be at most we should also keep in mind that we might want to try some kind of brute force (where we try the numbers from 1 to or from to , which should pass the 1.5 second time-limit).
So these two separate ideas (finding a mathematical formula + some kind of brute force) make up the initial observations.
Next we ask the question: "How do we find a formula?" To do this we need to exploit the structure of the problem. We need to find out "what makes a number nice" and "how does the sum of nice numbers look?" At this point, it is best to write out the information we have, and see if we can come to something that works. As a "problem-solving technique", this is when it helps to work backwards. In particular, we start by assuming we have a nice number , and asking what properties of make this problem easy.
So we try this. Let be an arbitrary nice number. Since the definition of niceness depends on the "quotient" and "remainder" upon division by , we will write out where is the quotient and is the remainder (). Now, we notice that the definition of "nice", using and is exactly as follows:
where is some integer between and . Rewriting this without the fractions gives us:
We can play around with the formula a bit more and we notice that, since and , we can substitute this second formula into the first formula, and we get that
Now, let's stop and think. We notice that this formula for really only depends on and (since is a constant, given as input). In particular, if we know and then we know . But does this work with any and ? Well, by definition of (being the remainder), we know that . But we also know that (this was somewhere in the definition of "nice"). So altogether we know that
are all the possible choices of . Similarly, almost by definition of "nice", we know the integer must satisfy:
And more importantly, if we pick any and that are in these ranges, then we uniquely get an by setting .
(Note: In combinatorics, this is often called a "bijection" or a "one-to-one correspondence". We can think of the set of nice values as equivalent to the set of pairs , with and .)
Now, we have a nice formula involving nice numbers! Let's focus on the key problem: Finding the sum of all the nice numbers. It turns out that we can solve this by writing out the formula as well, this time remembering the "one-to-one correspondence" we just found. That is:
That is, our answer is just the sum of over all choices of and , since each of these pairs gives us a nice number.
From here, the rest is just algebra. We now want to simplify the formula we just came up with above by expanding it and then "collecting similar terms". I personally just wrote this all down on paper until I came to a nice simple formula. The derivation follows. (Beware, it may actually be easier for the reader to do the algebra independently). Here is the full derivation:
Of course, being able to do this algebra requires some basic experience with equations and algebra. We also used the fact that:
This was used a couple times in the derivation above. It comes up fairly often, so it's nice to remember this identity. You should also be familiar with factoring and dealing with "sigma" () notation. Also, be careful about "off-by-one" errors! (During the contest, I almost had a bug because I accidentally wrote instead of somewhere in the formula. I found it though...)
Anyway, all-in-all, we notice that the final formula has no summations in it, and no variables except and (which are given constants). So this is enough to solve the problem; Given and , we just print out the last line of that formula, and we're done! (It turns out we didn't need to use brute force!)
Note: Be careful with overflow. Since and are big, doing these multiplications will overflow a 32 bit integer. It may even overflow a 64-bit integer if you are not careful. So make sure to take everything modulo multiple times in that formula above. But over all, this is the key idea.
-
If we write where is the quotient and , then is nice if and only if: and (with ). We found this by working backwards, and it led to a nice characterization of the nice numbers.
-
There is a unique nice number corresponding to each pair of integers with and . In particular, this number is: . This was the "bijection" that made it easy to write out the sum later.
-
The final sum of all nice numbers is: . We found this by writing out the formula, using the bijection we described above. With a little bit of algebra, we eventually came to this nice formula that only includes and , with no other variables in it. This gives us a constant time solution.
-
for any integer . This is a basic algebraic identity related to the "Triangular Numbers". It was used a couple times when simplifying the formula.
-
The definition of nice was particularly constructed by the authors to make the problem solvable. In general, it's good to focus on the structure of the problem in order to find observations. In this case, we focused on the "divisibility", including the relationship between the "quotient" and the "remainder" .
-
In many "math" problems, the end-result is to find a nice formula. In general, writing down observations, variable names, equations and facts can make it easier to find the answer in the end. In this problem, we specifically wrote down:
-
-
-
and
-
the summation formula and its derivation
The only time you shouldn't write everything down is when you are an International Grandmaster and you can solve this problem in a couple minutes! (Maybe even the International Grandmasters still spend some time to write down their formulas.) Otherwise, writing down facts helps to ease the load on your brain, and also makes it very easy to see patterns and formulas. 3. In this problem we were asked to find the sum of nice numbers. Instead we started by focusing on the properties of the nice numbers themselves, and this quickly led to a formula about the sum. 4. Transforming the variable into the pair of variables was a useful idea. We did not know right away that it would be useful, but it allowed us to write down the formula in a nice way in the end. 5. The last part of the problem was to simplify the formula we came up with. Once we found it in terms of and , we were done. 6. This problem can be really easy if you have experience with mathematics, algebra and formulas. It also helps to be able to classify problems. When I read this problem, my initial observations pointed me in the right direction because I expected to look for a formula, since it was a "Math" problem. Problem-solving experience such as this can speed up the process as well (for example, see the Problem Classification section below)
-
This problem would have been really hard if you didn't think to write out the formula. It might have also been hard if you didn't think of writing out the variable as a quotient and remainder, as . As a learning point, we recognize that writing out variables and observations can often be helpful, as long as we don't spend too much time working through the algebra.
-
Mathematics / Formulas and Proofs / Algebra and Simplification
-
Combinatorics / Counting / Summation over a Set
-
Brute force
-
Greedy / Observations / Exploit Structure and Conditions
Suppose you didn't know the identity . Can you think of a linear time algorithm to solve the problem? (You can still use any of the other observations from above if you want to.)
Let's change the definition of nice. What if a nice number is one where is an integer between and ? How would we solve this modified problem? Can we still find a nice formula for the sum of the nice numbers? What is it? If not, can we do a brute force this time? How, or why not? All-in-all, come up with some algorithm to solve this modified problem: Find the sum of all "modified" nice numbers.
Find an identity for for any integer .
Codeforces Round #273 (Div. 2), Problem B - Random Teams. A bit more practice with binomial coefficients and math.