Codeforces Round #395 (Div. 2) E(数论)
来源:互联网 发布:如何选葫芦丝知乎 编辑:程序博客网 时间:2024/05/18 09:13
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.
The first line contains two integers m and n (2 ≤ m ≤ 109 + 7, 1 ≤ n ≤ 105, m 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.
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.
17 50 2 4 13 15
13 2
17 50 2 4 13 14
-1
5 31 2 3
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;}
- Codeforces Round #395 (Div. 2) E(数论)
- Codeforces Round #325 (Div. 2) E 数论
- Codeforces Round #257 (Div. 2)E(数论+构造)
- Codeforces Round #395 (Div. 2)E: Timofey and remoduling(数学+数论)
- Codeforces Round #362 (Div. 2) E. PLEASE(数论 + 递推)
- Codeforces Round #226 (Div. 2) C 数论
- 【数论】Codeforces Round #334 (Div. 2) D
- Codeforces Round #201 (Div. 2) C 数论
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces Round #103 (Div. 2) E题
- Codeforces Round #147 (Div. 2) E
- Codeforces Round #184 (Div. 2) D、E
- Codeforces Round #197 (Div. 2) (C~E)
- Codeforces Round #102 (Div. 2) //缺E
- Codeforces Round #104 (Div. 2) //缺E
- Codeforces Round #105 (Div. 2) //缺E
- Codeforces Round #103 (Div. 2) //缺E
- Codeforces Round #106 (Div. 2) //缺E
- 基于RISC-V架构的开源处理器及SoC研究综述(一)
- Gabor滤波简介与Opencv中的实现及参数变化实验
- 如何获取Android手机底部导航栏的高度
- 演员
- 设计模式-简单工厂模式
- Codeforces Round #395 (Div. 2) E(数论)
- C语言剖析
- Codeforce - 262 - B. Roma and Changing Signs
- bzoj 3611 [Heoi2014]大工程 虚树DP
- IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- 【Python】学习笔记——-4.4、生成器
- RxJava+Retrofit+okhttp+mvp+butterknife实现的简易开源项目
- 10分钟快速上手github
- (Mysql 四)普通索引(BTREE索引)的使用