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 ...
随机推荐
- mac 安装requests
首先mac上已经安装了python,我的是python2x,我自己安装了python3,python3安装requests,控制台,输入,pip3 install requests 下面就已经安装完成 ...
- windows程序内部运行机制
Windows程序内部运行机制 2007-10-21 19:52 1010人阅读 评论(0) 收藏 举报 windowsvc++applicationcallbackwinapistructure W ...
- Python3爬虫(十二) 爬虫性能
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.简单的循环串行一个一个循环,耗时是最长的,是所有的时间综合 import requests url_list ...
- git之解决冲突
前面几次使用git,一直对于冲突的这个问题不是很理解,感觉有些时候就会产生冲突,在此记录一下解决冲突的流程 1.git bash上面冲突显示 2.在idea上面可以看到冲突的文件 3.去解决冲突 4. ...
- 初步学习pg_control文件之三
接前文,初步学习pg_control文件之二 继续学习: 研究 DBState,先研究 DB_IN_PRODUCTION ,看它如何出现: 它出现在启动Postmaster时运行的函数处: /* * ...
- java集合浅谈(一)
一.类库结构图概览 容器对象仅能持有对象引用(对象的指针),而不是Copy对象信息,从网上搜得几张Java中集合类库的结构图,如下所示: 二.解说Collection 2.1 Collection ( ...
- git安装后Gitbase闪退,gui无法使用问题解决
一般是因为null.sys导致,根本原因应该还是你装的盗版系统有问题,解决办法如下 cmd 打开命题提示符后 输入 sc start null 看 null.sys是否有问题,如果有问题,重新 ...
- cocos2d-x 场景切换
场景切换的方法 场景切换是通过导演类director实现的,其中的相关方法如下: director.run(new_scene).该方法可以运行场景,只能在启动第一个场景时调用该方法.如果已运行场景, ...
- 使用CodeBlocks为你的程序添加程序文件图标和启动读入图标
其实也非常简单,自己这两天用win32api做了一个小程序,可是发现图标却是dos的,太难看了,于是就想起以前学win32汇编时候用到的工具,ResEd,已经被我汉化了一些,估计有新的版本发布吧,但是 ...
- 1107 Social Clusters (30 分)(并查集)
并查集的基本应用 #include<bits/stdc++.h> using namespace std; ; vector<int>vec[N]; int p[N]; con ...