← All problems

Dreamoon And Sums

Codeforces · Round #272 (Div. 1) · A ↗

Problem

Given integers aa and bb (with 1≤a,b≤1071 \leq a,b \leq 10^7) we say that another integer xx is nice if mod(x,b)≠0mod(x,b) \neq 0 and div(x,b)mod(x,b)\frac{div(x,b)}{mod(x,b)} is an integer between 11 and aa.

We are given aa and bb. Find the sum of all nice integers. (If the answer is too large, print it modulo 1,000,000,0071,000,000,007.)

(Please see the problem statement for further clarification)

Initial Observations
  1. Math, Formula
  2. Divisibility, Quotient, Remainders
  3. "Write out" the formula
  4. aa and bb are medium sized
  5. Brute force, somehow
  6. Summing, counting

Review

Upon looking at the problem, we notice that it will likely be a math problem. Because the numbers (aa, bb, 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 aa and bb can be at most 10710^7 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 aa or from 11 to bb, 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 xx, and asking what properties of xx make this problem easy.

So we try this. Let xx be an arbitrary nice number. Since the definition of niceness depends on the "quotient" and "remainder" upon division by bb, we will write out x=qb+rx = qb + r where qq is the quotient and rr is the remainder (0≤r<b0 \leq r \lt b). Now, we notice that the definition of "nice", using qq and rr is exactly as follows:

x is nice if and only if qr=k and r≠0x \text { is nice if and only if } \frac{q}{r} = k \text{ and } r \neq 0

where kk is some integer between 11 and aa. Rewriting this without the fractions gives us:

q=krq = kr

We can play around with the formula a bit more and we notice that, since x=qb+rx = qb + r and q=krq = kr, we can substitute this second formula into the first formula, and we get that

x=(q)b+r=(kr)b+r=r(kb+1)\begin{array}{rcl} x & = & (q)b + r \\ & = & (kr)b + r \\ & = & r(kb + 1) \end{array}

Now, let's stop and think. We notice that this formula for xx really only depends on kk and rr (since bb is a constant, given as input). In particular, if we know kk and rr then we know xx. But does this work with any kk and rr? Well, by definition of rr (being the remainder), we know that 0≤r<b0 \leq r \lt b. But we also know that r≠0r \neq 0 (this was somewhere in the definition of "nice"). So altogether we know that

1≤r≤b−11 \leq r \leq b-1

are all the possible choices of rr. Similarly, almost by definition of "nice", we know the integer kk must satisfy:

1≤k≤a1 \leq k \leq a

And more importantly, if we pick any kk and rr that are in these ranges, then we uniquely get an xx by setting x=r(kb+1)x = r(kb + 1).

(Note: In combinatorics, this is often called a "bijection" or a "one-to-one correspondence". We can think of the set of nice xx values as equivalent to the set of pairs (k,r)(k,r), with 1≤k≤a1 \leq k \leq a and 1≤r≤b−11 \leq r \leq b-1.)

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:

(sum of nice numbers)=∑nice xx=∑r∑k(r(kb+1))\text{(sum of nice numbers)} = \sum_{\text{nice } x}{x} = \sum_{r}\sum_{k}\left(r(kb+1)\right)

That is, our answer is just the sum of r(kb+1)r(kb+1) over all choices of rr and kk, 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:

∑r=1b−1∑k=1a(r(kb+1))=∑r=1b−1r⋅[∑k=1a(kb+1)]=∑r=1b−1r⋅[∑k=1a(kb)+∑k=1a(1)]=∑r=1b−1r⋅[b∑k=1a(k)+a]=∑r=1b−1r⋅[b(1+2+3+⋯+a)+a]=∑r=1b−1r⋅[b⋅a(a+1)2+a]=[b⋅a(a+1)2+a](∑r=1b−1r)=[b⋅a(a+1)2+a](1+2+3+⋯+(b−1))=[b⋅a(a+1)2+a](b(b−1)2)\begin{array}{rcl} \sum_{r=1}^{b-1} \sum_{k=1}^{a} \left(r(kb+1)\right) & = & \sum_{r=1}^{b-1} r \cdot \left[\sum_{k=1}^{a} \left(kb + 1 \right) \right] \\ & = & \sum_{r=1}^{b-1} r \cdot \left[\sum_{k=1}^{a} \left(kb\right) + \sum_{k=1}^{a} \left(1\right) \right] \\ & = & \sum_{r=1}^{b-1} r \cdot \left[b\sum_{k=1}^{a} \left(k\right) + a \right] \\ & = & \sum_{r=1}^{b-1} r \cdot \left[b\left(1 + 2 + 3 + \cdots + a\right) + a \right] \\ & = & \sum_{r=1}^{b-1} r \cdot \left[\frac{b\cdot a(a+1)}{2} + a \right] \\ & = & \left[\frac{b\cdot a(a+1)}{2} + a \right]\left(\sum_{r=1}^{b-1} r \right) \\ & = & \left[\frac{b\cdot a(a+1)}{2} + a \right]\left(1 + 2 + 3 + \cdots + (b-1) \right) \\ & = & \left[\frac{b\cdot a(a+1)}{2} + a \right]\left(\frac{b(b-1)}{2} \right) \\ \end{array}

Of course, being able to do this algebra requires some basic experience with equations and algebra. We also used the fact that:

∑i=1ni=(1+2+3+⋯+n)=n(n+1)2.\sum_{i=1}^{n}{i} = (1 + 2 + 3 + \cdots + n) = \frac{n(n+1)}{2}.

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" (σ\sigma) notation. Also, be careful about "off-by-one" errors! (During the contest, I almost had a bug because I accidentally wrote b+1b+1 instead of b−1b-1 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 aa and bb (which are given constants). So this is enough to solve the problem; Given aa and bb, 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 aa and bb 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 1,000,000,0071,000,000,007 multiple times in that formula above. But over all, this is the key idea.

Key observations:

References

Problem-solving techniques used:

Exploit the Constraints

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" qq and the "remainder" rr.

Write It Out

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:

  1. x=qb+rx = qb + r
  2. q=krq = kr
  3. 1≤k≤a1 \leq k \leq a and 1≤r≤b−11 \leq r \leq b-1
  4. 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.

Work Backward

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.

Problem Transformation

Transforming the variable xx into the pair of variables (r,k)(r,k) 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.

Problem Simplification

The last part of the problem was to simplify the formula we came up with. Once we found it in terms of aa and bb, we were done.

Draw from Experience

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)

Learning points:

Topics:

Related problems: