Problem Summary
Given N positive integers, d[1],d[2],…d[N] (1<=N<=10, 1<=d[i]<=256), compute the largest impossible number that cannot be written as a[1]d[1]+a[2]d[2]+…+a[n]*d[n] (a[i] is non-negative integer, 1<=i<=N).
If this number does not exist, print 0.
Analysis & Solution
Only in two cases, the number does not exist:
- d[1],d[2],…,d[N] are not relatively prime. That is,the greatest common divisor of d[1],…,d[N] is greater than 1.
- The minimum of d[1],d[2],…,d[N] is 1.
After checking for the two cases, we can use dynamic programming to solve this. Still, there are some tricks about the upper bound of the answer:
- Apparently, if the minimum of d[1],…,d[N] is m, and there are m consecutive numbers, then all the numbers from here on are possible.
- If m and n are releatively prime, then the largest number we cannot make with them is B = m*n-m-n. Moreover, if m and n are not relatively prime, the answer cannot exceed B. If there is a third number l, then the upper bound must be equal to or less than B.
Code
|
|