SRM 398 DIV2 [250]

本文介绍了一个算法问题,通过递推公式生成序列,并找出序列中任意两元素间的最小绝对差。使用C++实现,包括序列生成、排序及遍历比较等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include
<algorithm>
#include
<vector>
using namespace 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值