CF763C Timofey and Remoduling
题目戳这里。
这道题目纯粹是考思维。
若\(2N \le M\),由于答案肯定是\(s,s+d,\dots,s+(N-1)d\),我们任意枚举两个数\(a,b\),不妨设\(b\)在数列中出现在\(a\)后面\(k\)位,设\(g = b-a\),则\(g\)这个差在所有数出现刚好\(N-K\)次。我们任取个\(g\),用二分或哈希求个差出现次数,就可以得知\(k\)了,然后\(d = gk^{-1}\)。在检验数列中有\(a\)的公差为\(d\)的等差数列是否存在即可。
若\(2N > M\),我们考虑这些数的补集即可,这样就可以求出\(d\)了。
然后为什么\(2N > M\)不能用第一种情况来做呢?因为\(kd\)这个差不一定出现\(N-k\)次。因为假设我枚举到的差是\((N-1)d\),那么\(s+(2N-2)d\)这个数有可能在模\(M\)意义下是在数列中的,但是这个数字又是不合法的。
程序实现还有一些细节,可以参考一下代码。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn = 100010;
int M,N,A[maxn],B[maxn],ans1,ans2;
inline int gi()
{
char ch; int ret = 0,f = 1;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1,ch = getchar();
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
}
inline ll qsm(ll a,int b)
{
ll ret = 1;
for (;b;b >>= 1,(a *= a) %= M) if (b&1) (ret *= a) %= M;
return ret;
}
inline bool find(int *a,int n,int x) { return a[lower_bound(a+1,a+n+1,x)-a] == x; }
inline void solve(int *a,int n)
{
if (n == 1) { ans1 = a[1],ans2 = 1; return; }
int tmp = a[2]-a[1],cnt = 0,tot = 1;
for (int i = 1;i <= n;++i) cnt += find(a,n,(a[i]+tmp)%M);
ans2 = qsm(n-cnt,M-2)*tmp%M;
for (int now = a[1],nx;;now = nx,++tot)
{
nx = now+ans2; if (nx >= M) nx -= M;
if (!find(a,n,nx)) break;
}
for (int now = a[1],nx;;now = nx,++tot)
{
ans1 = now; nx = now-ans2; if (nx < 0) nx += M;
if (!find(a,n,nx)) break;
}
if (tot != n) ans1 = -1;
}
int main()
{
freopen("763C.in","r",stdin);
freopen("763C.out","w",stdout);
M = gi(); N = gi();
for (int i = 1;i <= N;++i) A[i] = gi();
sort(A+1,A+N+1);
if (N == 1||N == M) printf("%d 1\n",A[1]);
else
{
if (2*N <= M) solve(A,N);
else
{
int n = 0;
for (int i = 0;i < M;++i) if (!find(A,N,i)) B[++n] = i;
solve(B,n);
if (ans1 != -1) { ans1 += (ll)n*ans2%M; if (ans1 >= M) ans1 -= M; }
}
if (ans1 == -1) puts("-1");
else printf("%d %d\n",ans1,ans2);
}
fclose(stdin); fclose(stdout);
return 0;
}
CF763C Timofey and Remoduling的更多相关文章
- [CodeForces-763C]Timofey and remoduling
题目大意: 告诉你一个长度为n的等差数列在模m意义下的乱序值(互不相等),问是否真的存在满足条件的等差数列,并尝试构造任意一个这样的数列. 思路: 首先我们可以有一个结论: 两个等差数列相等,当且仅当 ...
- 763A - Timofey and a tree
A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- Codeforces_764_C. Timofey and a tree_(并查集)(dfs)
C. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 【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 ...
随机推荐
- mysql数据库的基本使用命令总结
mysql数据库是一个常用的关系型数据库 关系型数据库核心元素有哪些? 主键:特殊字段,用来唯一标识记录的唯一性 字段:数据列 记录:数据行 数据表:数据行的集合 数据库:数据表的集合 安装.启动.停 ...
- laravel 增删改查 数据库设置 路由设置
laravel 框架的路由设置: url: http://www.shanzezhao.com/laraverl/my_laravel/public/index.php/indexs laravel ...
- PHP学习课程和培训方向学习路线分享
php语言的优越性,集结了很多的开发爱好者,无论行业前景和个人发展来说,php正飞速的发展,php在不断兼容着类似closures和命名空间 等技术,同时兼顾性能和当下流行的框架.版本是7之后,一直在 ...
- 007---logging日志模块
logging模块 用途:服务器运行日志.运维日志... import logging from logging.handlers import RotatingFileHandler, TimedR ...
- Tensorflow之MNIST的最佳实践思路总结
Tensorflow之MNIST的最佳实践思路总结 在上两篇文章中已经总结出了深层神经网络常用方法和Tensorflow的最佳实践所需要的知识点,如果对这些基础不熟悉,可以返回去看一下.在< ...
- 转载:Linux系统和Linux系统之间如何实现文件传输
两台Linux系统之间传输文件 听语音 | 浏览:13183 | 更新:2014-07-15 15:22 | 标签:linux 1 2 3 4 5 6 分步阅读 如何在Linux系统之间传输文件及文件 ...
- 4269: 再见Xor
4269: 再见Xor 链接 分析: 和SGU 275唯一不同的就是需要求出次小值,那么异或出最大值的所有元素中,找到最小的,去除即可. 代码: #include<bits/stdc++.h&g ...
- Django-Content-type用法
from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...
- html简单的分享功能
超级简单的分享. 包括:QQ.QQ空间.新浪微博.腾讯微博,微信(只是一个二维码): 1.首先是html代码: (前端我并不太会,一直用的都是bootstrap) <div class=&quo ...
- nginx https ssl 配置
#设置https 访问server { listen ; server_name www.xxx.com; access_log xxx/xxx/xxx.log combined; index ind ...