Codeforces Round #395 (Div. 2) E(数论)

E. Timofey and remoduling
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey likes to look for arithmetical progressions everywhere.

One of his birthday presents was a sequence of distinct integers a1, a2, ..., an. Timofey wants to know whether he can rearrange the elements of the sequence so that is will be an arithmetical progression modulo m, or not.

Arithmetical progression modulo m of length n with first element x and difference d is sequence of integers x, x + d, x + 2d, ..., x + (n - 1)·d, each taken modulo m.

Input

The first line contains two integers m and n (2 ≤ m ≤ 109 + 71 ≤ n ≤ 105m is prime) — Timofey's favorite prime module and the length of the sequence.

The second line contains n distinct integers a1, a2, ..., an (0 ≤ ai < m) — the elements of the sequence.

Output

Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo m.

Otherwise, print two integers — the first element of the obtained progression x (0 ≤ x < m) and its difference d (0 ≤ d < m).

If there are multiple answers, print any of them.

Examples
input
17 5
0 2 4 13 15
output
13 2
input
17 5
0 2 4 13 14
output
-1
input
5 3
1 2 3
output
3 4


题意:

给出n和一个质数m

然后是n个数a1,a2...an.

问是否有一个等差数列x,x+d...,x+(n-1)*d,使得其每个元素对m取模后,结果为a1,a2,...,an(重排后)。如果有,输出首项x和公差d。如果没有,则输出-1,。多解输出一个即可。


题解:

这个题解很详细:传送门


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1e5+10;
ll ans1,ans2;
ll a[maxn],b[maxn];
ll fp(int n,ll k,ll m)
{
    ll ans=1,t=n;
    while(k)
    {
        if(k&1) ans=(ans*t)%m;
        t=(t*t)%m;
        k/=2;
    }
    return ans%m;
}
//a1=(sn-n*(n-1)/2*d)/n
//Sn^2=n(a1)^2+n(n-1)(2n-1)d^2/6+n(n-1)*d*a1  两个数学公式的运用
int main()
{
    int n;
    ll m;
    scanf("%lld%d",&m,&n);
    ans1=ans2=0;
    rep(i,1,n+1)
    {
        scanf("%lld",&a[i]);
        a[i]%=m;
        ans1=(ans1+a[i])%m;
        ans2=(ans2+a[i]*a[i]%m)%m;
    }
    if(n==1)
    {
        printf("%lld 0\n",a[1]);
        return 0;
    }
    sort(a+1,a+n+1);
    rep(i,2,n+1)
    {
        ll d=(a[i]-a[1])%m;
        ll x=(ans1-1ll*n*(n-1)/2%m*d%m+m)%m*fp(n,m-2,m)%m;
        ll res=(n*x%m*x%m+1ll*n*(n-1)*(2*n-1)/6%m*d%m*d%m+1ll*n*(n-1)%m*d%m*x%m)%m;
        if(res==ans2)
        {
            b[1]=x;
            rep(j,2,n+1)
            {
                b[j]=(b[j-1]+d)%m;
            }
            sort(b+1,b+n+1);
            bool f=true;
            rep(j,1,n+1)
            {
                if(a[j]!=b[j])
                {
                    f=false;
                    break;
                }
            }
            if(f)
            {
                printf("%lld %lld\n",x,d);
                return 0;
            }
        }
    }
    puts("-1");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值