[CodeForces-763C]Timofey and remoduling
题目大意:
告诉你一个长度为n的等差数列在模m意义下的乱序值(互不相等),问是否真的存在满足条件的等差数列,并尝试构造任意一个这样的数列。
思路:
首先我们可以有一个结论:
两个等差数列相等,当且仅当数字和与平方和分别相等。
首先求出一开始的数字和和平方和。
然后我们枚举每一个数作为首项的情况,求出这个数作为首项以后的数字和和平方和,根据数字和求出公差,然后用平方和检验一下。
然而这样并不能保证一定正确,但至少有大概率是正确的,我们可以O(n)的时间检验一下。
注意特判n=m的情况。
#include<cstdio>
#include<hash_set>
typedef long long int64;
inline int getint() {
register char ch;
while(!__builtin_isdigit(ch=getchar()));
register int x=ch^'';
while(__builtin_isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
__gnu_cxx::hash_set<int> set;
const int64 N=;
int64 a[N];
int64 m,n;
inline int64 sqr(const int64 &x) {
return x*x%m;
}
void exgcd(const int64 &a,const int64 &b,int64 &x,int64 &y) {
if(!b) {
x=;
y=;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline int64 inv(const int64 &x) {
int64 tmp,ret;
exgcd(x,m,ret,tmp);
return (ret+m)%m;
}
int main() {
m=getint(),n=getint();
if(n==m) {
__builtin_puts("0 1");
return ;
}
int64 sum0=,sqrsum0=;
for(register int64 i=;i<n;i++) {
a[i]=getint();
set.insert(a[i]);
sum0=(sum0+a[i])%m;
sqrsum0=(sqrsum0+sqr(a[i]))%m;
}
const int64 c=inv(n*(n-)/);
for(register int64 i=;i<n;i++) {
const int64 sum=((sum0-a[i]*n%m)%m+m)%m;
const int64 sqrsum=((sqrsum0-sqr(a[i])*n%m-a[i]*sum%m*%m)%m+m)%m;
const int64 d=sum*c%m;
if(n*(n-)*(n*-)/%m*sqr(d)%m!=sqrsum) continue;
int64 tmp=a[i];
for(register int64 i=;i<n;i++) {
tmp=(tmp+d)%m;
if(!set.count(tmp)) goto Next;
}
__builtin_printf("%I64d %I64d\n",a[i],d);
return ;
Next:;
}
__builtin_puts("-1");
return ;
}
[CodeForces-763C]Timofey and remoduling的更多相关文章
- CF763C Timofey and Remoduling
题目戳这里. 这道题目纯粹是考思维. 若\(2N \le M\),由于答案肯定是\(s,s+d,\dots,s+(N-1)d\),我们任意枚举两个数\(a,b\),不妨设\(b\)在数列中出现在\(a ...
- Codeforces 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- Codeforces 764C Timofey and a tree
Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that th ...
- CodeForces - 764B Timofey and cubes(模拟)
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Ev ...
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
地址:http://codeforces.com/contest/764/problem/D 题目: D. Timofey and rectangles time limit per test 2 s ...
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
地址:http://codeforces.com/contest/764/problem/C 题目: C. Timofey and a tree time limit per test 2 secon ...
- Codeforces Round #395 (Div. 2)B. Timofey and cubes
地址:http://codeforces.com/contest/764/problem/B 题目: B. Timofey and cubes time limit per test 1 second ...
- 【codeforces 764B】Timofey and cubes
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 旋转3D立方体
<!DOCTYPE html><html><head> <title>css-3d-盒子</title> <meta charset= ...
- HDU 1711 Number Sequence (字符串处理 KMP)
题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...
- bzoj 1934最小割
比较显然的最小割的题,增加节点source,sink,对于所有选1的人我们可以(source,i,1),选0的人我们可以(i,sink,1),然后对于好朋友我们可以连接(i,j,1)(j,i,1),然 ...
- 福建工程学院寒假作业第一周G题
涨姿势题1 TimeLimit:1000MS MemoryLimit:128000KB 64-bit integer IO format:%lld 涨姿势题就是所谓的优化题,在组队赛中,队伍发现 ...
- linux内存分配方法总结【转】
转自:http://www.bkjia.com/Linuxjc/443717.html 内存映射结构: 1.32位地址线寻址4G的内存空间,其中0-3G为用户程序所独有,3G-4G为内核占有. 2.s ...
- elk系列7之通过grok分析apache日志【转】
preface 说道分析日志,我们知道的采集方式有2种: 通过grok在logstash的filter里面过滤匹配. logstash --> redis --> python(py脚本过 ...
- CCTF部分赛题分析
这次算是跟着师傅们全程打完了CCTF的线上赛,一些强队的WriteUp也放了出来.这篇文章主要是想跟着大牛的思路把那些题重新再过一遍. PWN3 这个是格式化字符串漏洞的题.printf的格式化串直接 ...
- Windows实现内网IPMI端口转发
https://www.cnblogs.com/yunweis/p/8024346.html
- 安装 jupyter notebook 出现 ModuleNotFoundError: No module named 'markupsafe._compat' 错误
使用 python -m pip install jupyter 安装完成 jupyter notebook 之后,在命令行界面输入 "jupyter notebook "指令打开 ...
- 面试题30:最小的K个数
方法一:利用partition void GetLeastNumbers_Solution1(int* input, int n, int* output, int k) { || k <= ) ...