TopCoder problem "MinDifference" used in SRM 398 (Division II Level One)



Problem Statement

    

You are given numbers A0, X, Y, M and n. Generate a list A of length n according to the following recurrence relation:

A[0] = A0

A[i] = (A[i - 1] * X + Y) MOD M, for 0 < i < n



Return the minimal absolute difference between any two elements of A.

 

Definition

    
Class:MinDifference
Method:closestElements
Parameters:int, int, int, int, int
Returns:int
Method signature:int closestElements(int A0, int X, int Y, int M, int n)
(be sure your method is public)
    
 

Constraints

-A0, X, Y, M will each be between 1 and 10000, inclusive.
-n will be between 2 and 10000, inclusive.
 

Examples

0)
    
3
7
1
101
5
Returns: 6
The elements of the list are {3, 22, 54, 76, 28}. The minimal difference is between elements 22 and 28.
1)
    
3
9
8
32
8
Returns: 0
All elements are the same.
2)
    
67
13
17
4003
23
Returns: 14
3)
    
1
1221
3553
9889
11
Returns: 275
4)
    
1
1
1
2
10000
Returns: 0
5)
    
1567
5003
9661
8929
43
Returns: 14

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=12170&pm=8161

Writer:

boba5551

Testers:

PabloGilberto , legakis , Olexiy , ivan_metelsky

Problem categories:

Simple Search, Iteration, Sorting