SRM 480, D2, 250-Pointer (Cryptography)

James S. Plank

Thu Dec 20 12:27:25 EST 2018

In case the Topcoder servers are down

Here is a summary of the problem:

Examples

These are all included in main.cpp. If you give the input "-" to main.cpp, then it will read its values from standard input.

0{ 1, 2, 3 }12
1{ 1, 3, 2, 1, 1, 3 }36
2{1000, 999, 998, 997, 996, 995}986074810223904000
3{ 1, 1, 1, 1 }2

Testing Yourself without Topcoder's Servers

The workflow that I use for Topcoder is as follows:

Hints

Try this first on your own. If you get stuck or frustrated, then come back here and scroll down.


















































First off, you need to figure out -- given a collection of numbers, which number do I increment so that the product is increased by the largest amount? It shouldn't be too hard to see that you need to increment the smallest number.

[If you want to prove that formally, you can. Suppose you have a collection of numbers whose product is P. And suppose you have two numbers in the collection, a and b such that a < b. If you increment a, you change the product from P to P(a+1)/a. If you increment b, you change the product to P(b+1)/b. Since a < b, we know that (a+1)/a > (b+1)/b. (If you are stuck on that, think about examples: 3/2 is definitely greater than 101/100, right?) That means incrementing a will increase the factor by the greatest amount.]

So, you need to find the minimum element and increment it. You can write your own code to do that in two lines. Or you can use the sort() method from C++'s algorithms package to sort the vector and then increment the first element.

Now how about calculating the product? Since you are told that the product will be ≤ 262, you need to use a 64-bit integer. In C++, this is a long long. Calculate your product using that, and you're done.