Codeforces Round #709 (Div. 2) B

该博客讨论了一个竞赛获奖者Alex面临的数组销售问题。参赛者需要确定给定数组是否能由特定的生成器规则(n, m, c, s)产生,并找到最大化m的值。题目给出了输入输出示例及解题思路,通过分析相邻元素差值来确定可能的c和m。代码实现用于检查是否存在这样的四元组并找出最大m。
摘要由CSDN通过智能技术生成

B. Restore Modulo

原题链接

题面

For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.

This generator takes four non-negative numbers n, m, c, s. n and m must be positive, s non-negative and for c it must be true that 0≤c<m. The array a of length n is created according to the following rules:

a1=smodm, here xmody denotes remainder of the division of x by y;
ai=(ai−1+c)modm for all i such that 1<i≤n.
For example, if n=5, m=7, c=4, and s=10, then a=[3,0,4,1,5].

Price of such an array is the value of m in this generator.

Alex has a question: how much money he can get for each of the arrays. Please, help him to understand for every array whether there exist four numbers n, m, c, s that generate this array. If yes, then maximize m.

input

The first line contains a single integer t (1≤t≤105) — the number of arrays.

The first line of array description contains a single integer n (1≤n≤105) — the size of this array.

The second line of array description contains n integers a1,a2,…,an (0≤ai≤109 ) — elements of the array.

It is guaranteed that the sum of array sizes does not exceed 105.

Output

For every array print:

−1, if there are no such four numbers that generate this array;
0, if m can be arbitrary large;
the maximum value m and any appropriate c (0≤c<m) in other cases.

Example

Input

6
6
1 9 17 6 14 3
3
4 2 2
3
7 3 4
3
2 2 4
5
0 1000000000 0 1000000000 0
2
1 1

Output

19 8
-1
-1
-1
2000000000 1000000000
0

题意

给出四个数n,m,c,s。按照:
a1=s%m
ai=(ai−1+c)%m (c<m)
以上两个条件一次表示a[i]从0直到n

题目要求给出已经表示出的数组的值,让我们逆推出是否存在这么4个数,如果存在输入最大的m和适当的c,如果无限大输出0,不存在输出-1

思路

从题意c<m中我们容易得出,如果a[i]>=a[i-1],则a[i]-a[i-1]是一定等于c。
在这里插入图片描述

如果a[i] <= a[i-1],那么我们可以得出a[i-1] + c = m + a[i]所以m = a[i-1] - a[i] + c,由此我们可以设b = a[i-1] - a[i]
在这里插入图片描述
于是我们只要能够确定唯一的b和c就能确定m了。
(我靠这么简单的问题当时怎么就不会呢)呜呜

代码如下

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1e5 + 10;
typedef long long LL;
LL a[N];

int main(void)
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for(int i=0;i<n;i++)cin >> a[i];
        
        int flag = 1;
        
        LL b = -1,c = -1;
        
        for(int i=1;i<n;i++)
        {
            if(a[i]-a[i-1]>=0){
                if(c==-1)c = a[i] - a[i-1];
                else{
                    if(c!=a[i]-a[i-1]){
                        flag = 0;
                        break;
                    }
                }
            }else if(a[i] - a[i-1] <=0){
                if(b==-1)b = a[i-1] - a[i];
                else {
                    if(b!=a[i-1]-a[i]){
                        flag = 0;
                        break;
                    }
                }
            }
        }    
        
            if(flag==1){
                LL num = b+c;
                if(c==-1||b==-1||(c==0&&b==0))cout << "0" << endl;
                else {
                    sort(a,a+n);
                    if(num<=a[n-1])cout << "-1" << endl;
                    else cout << b+c << " " << c << endl;
                }
            }else cout << "-1" << endl;
    }
    
    
    
    return 0;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值