[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 ...
随机推荐
- MQTT协议及推送服务
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建 ...
- 微信小程序导航设置
"tabBar": { "backgroundColor": "#ffffff", "color": "#00 ...
- 在mac上安装ruby
1.先装RVM,一个多版本ruby环境的管理和切换工具 curl -sSL https://get.rvm.io | bash -s stable 会自动装上最新版.更新RVM版本:$ rvm get ...
- WordPress的SEO插件——WordPress SEO by yoast安装及使用
插件:WordPress SEO by yoast 使用方法: 做好网站SEO一直是站长们的愿望,说简单也简单,但是说难也难,因为需要注意的地方太多,一个不小心被百度K了你都不知道怎么回事.这里和大家 ...
- jsoup抓取网页报错UnsupportedMimeTypeException
今天在用Jsoup爬虫的时候两次遇到下面错误 Exception in thread "main" org.jsoup.UnsupportedMimeTypeException: ...
- SVM问题再理解与分析——我的角度
SVM问题再理解与分析--我的角度 欢迎关注我的博客:http://www.cnblogs.com/xujianqing/ 支持向量机问题 问题先按照几何间隔最大化的原则引出他的问题为 上面的约束条件 ...
- Linux 添加普通用户到 sudoers 文件
前言 Linux 的普通用户(uid >= 500)不具有某些命令的执行权限,为了执行较高权限的命令,一般有两种方法: 第一种是使用 su - 命令切换到 root 用户去执行: 另外一种方法是 ...
- openjudge-NOI 2.6-1996 登山
题目链接:http://noi.openjudge.cn/ch0206/1996/ 题解: 正反求两次LIS即可 #include<cstdio> #include<cstring& ...
- WPF之DataGrid--列的前台及后台实现
一.前台实现 在xaml里可以很轻松地实现一个如下图所示的DataGrid <StackPanel> <ComboBox Width="50" Horizonta ...
- UBuntu14.04 --vim安装YouCompleteMe插件
说明 我电脑的系统参数(用 uname -a命令查看)如下: Linux avyn-Lenovo --generic #-Ubuntu SMP Tue Mar :: UTC i686 i686 i68 ...