TopCoder problem "MagicStones" used in TCHS SRM 55 (Division I Level Two)



Problem Statement

    You and your friend have N different stones placed in a row. A permutation of N stones can be described by N numbers: x0, x1, ... , xN-1, where all xi are distinct and each xi is between 0 and N-1, inclusive. When a permutation is applied to the stones, stone placed at position i is moved to position xi. You tell a permutation of length N to your friend and every day he applies it to the stones until they are situated in the same order as in the beginning. You want your friend to spend as many days as possible, so you select the permutation which maximizes the number of days. Given N, return the number of days your friend will have to move the stones.
 

Definition

    
Class:MagicStones
Method:maximumDays
Parameters:int
Returns:int
Method signature:int maximumDays(int N)
(be sure your method is public)
    
 

Constraints

-N will be between 1 and 50, inclusive.
 

Examples

0)
    
1
Returns: 1
Here there's only one possible permutation.
1)
    
2
Returns: 2
There're two possible permutations. If you select the permutation that swaps two stones, then your friend will have to spend two days.
2)
    
5
Returns: 6
One of the optimal permutations is 1, 2, 0, 4, 3.
3)
    
29
Returns: 2520

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=9957

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13524&pm=9957

Writer:

Gluk

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

Dynamic Programming