Problem Statement
We have automated the manufacturing process so that random sized boxes are produced. We specify values a, p, and n and the machine produces n boxes with dimensions:
(a,a^2) (a^3,a^4) (a^5,a^6) ... (a^(2n-1),a^(2n))
where a^j denotes (a to the j power) mod p.
Create a class UnNestable that contains a method maxCount that is given the positive integral values a, p, and n and that returns the size of the largest unnestable collection of boxes, where a collection is unnestable if no two boxes from the collection can be nested.
The sequence of boxes can be generated by
int rv=1;
for(int i=0; i<n; i++){
rv = (rv*a)%p; int x=rv;
rv = (rv*a)%p; int y=rv;
//process this box, which has dimensions x and y
}
Definition
- Class:
- UnNestable
- Method:
- maxCount
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int maxCount(int a, int p, int n)
- (be sure your method is public)
Notes
- The sequence may cycle, resulting in many boxes of the same dimensions. Duplicate boxes do count in the size of an unnestable collection
Constraints
- p is between 2 and 2,000,000,000, inclusive.
- a is between 1 and p-1, inclusive.
- No power of a is divisible by p.
- a*p is less than or equal to 2,000,000,000
- n is between 1 and 10,000 inclusive
Examples
10
15
3
Returns: 3
The random generator produces 10, (10*10)%15=10, 10*10%15=10, ... so the The three boxes are (10,10), (10,10), and (10,10). The entire collection is unnestable.10
17
4
Returns: 2
The sequence is 10, 100%17=15, 150%17=14, 140%17=4, 40%17=6, ... so the 4 boxes are (10, 15), (4, 14), (6, 9) and (5, 16). The non-nesting pairs are (10, 15) with (5, 16), (4, 14) with (6, 9), and (6, 9) with (5, 16). No collection of 3 of these boxes is pairwise unnestable.3
1000
3
Returns: 1
The boxes are (3,9), (27,81), and (243,729). The first one is nestable in both the others, and the second one is nestable in the third one. So there is no way to choose 2 of them which are unnestable. Of course, any collection of size one has the property that no two boxes from the collection can be nested.3
104729
10000
Returns: 9