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

来源:互联网 发布:如何选葫芦丝知乎 编辑:程序博客网 时间:2024/05/18 09:13

题目链接
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 50 2 4 13 15
output
13 2
input
17 50 2 4 13 14
output
-1
input
5 31 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 secondtypedef 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