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 ...
随机推荐
- c# WebBrowser开发参考资料--杂七杂八
c# WebBrowser开发参考资料 http://hi.baidu.com/motiansen/blog/item/9e99a518233ca3b24aedbca9.html=========== ...
- iWebShop产品功能技术优势有什么?
iwebshop基于iweb si 框架开发,在获得iweb si 技术平台支持的条件下,iwebshop可以轻松满足用户量级百万至千万级的大型电子商务网站的性能要求.站点的集群与分布式技术(分布式计 ...
- 安装java 和 eclipse
昨天安装eclipse出现个问题, 安装完了创建第一个项目目录的时候弹窗报错an ......什么什么, 百度一堆没有用,后来发现是jdk12不支持,换了jdk8就可以了, 然后eclipse安装py ...
- tarnado源码解析系列一
目录 tarnado tarnado源码安装 tarnado测试程序 application类的解析 一. tarnado简介 最近在学习Python,无意间接触到的tarnado,感觉tarnado ...
- ctf题目writeup(3)
题目地址: https://www.ichunqiu.com/battalion 1. 这个是个mp3,给的校验是为了下载下来的. 下来之后丢进audicity中 放大后根据那个音块的宽度来确定是 . ...
- 【GUI】一、Swing外观框架BeautyEye使用
一.Swing外观框架BeautyEye使用 1.1 导包 BeautyEye.jar 1.2 使用BeautyEye L&F public static void main(String[] ...
- mac制作U盘启动器
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.所需工具及必要条件: 1. 首先需要一个大于16GB U盘. 2.电脑系统版本应该大于10.11.X(因为之前 ...
- 按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分
SELECT S# as 学生ID ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 数据库 ,(SELECT score FROM S ...
- .Net 面试题 汇总(一)
1.@page指令只能在_aspx___文件(填写扩展名)中使用,而@Control指令只能用在_ascx___文件(填写扩展名)中使用. 2.说明控件DataGrid,DataTable,DataV ...
- Service ANR问题
错误堆栈: ActivityManager: ANR in com.oppo.reader PID: 8071 Reason: executing service com.oppo.reade//co ...