#include<iostream> #include<algorithm> #include<vector> usingnamespace std; class MinDifference...{ public: int closestElements(int A0,int X,int Y,int M,int n) ...{ vector<int> ivec(n); vector<int>::iterator iter=ivec.begin(); *iter++=A0; for (;iter!=ivec.end();++iter) *iter=(*(iter-1)*X+Y)%M; sort(ivec.begin(),iter); iter=ivec.begin()+1; int min=*iter-*(iter-1); for (;iter!=ivec.end();++iter) if (*iter-*(iter-1)<min) min=*iter-*(iter-1); return min; } };
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
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.