The Frobenius Problem
Time limit1sMemory limit128 MB
Count the integers up to 1,000,000 that are not nonnegative combinations of four given numbers and report the largest such integer.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Number theory
- Solved
- No attempts yet
Problem
The Frobenius problem is an old problem in mathematics, named after the German mathematician G. Frobenius (1849-1917).
Let be integers larger than 1 whose greatest common divisor is 1. It is known that only finitely many integers greater than or equal to 0 cannot be written as with integer coefficients . The largest such integer is the Frobenius number of , written . So is the largest nonnegative integer that is not a nonnegative integer linear combination of .
For there is a simple formula for . For it is much more complicated. When , formulas are known only for special choices of , and for no formula is known at all.
This problem asks about the case . Four integers are given with and . Two things are asked.
- How many nonnegative integers less than or equal to 1,000,000 cannot be written as a nonnegative integer linear combination of , , and ?
- Is the Frobenius number of less than or equal to 1,000,000, and if it is, what is its value?
Input
The first line contains the number of test cases. Each test case has the following format.
- One line with four integers separated by single spaces ( and ).
Output
Print two lines for every test case.
- The first line contains the number of integers between 0 and 1,000,000 (both included) that cannot be written as with nonnegative integers .
- The second line contains the Frobenius number if it is less than or equal to 1,000,000, and -1 otherwise.