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 ...
随机推荐
- laravel 基础 --内置函数
简介 Laravel 自带了一系列 PHP 辅助函数,很多被框架自身使用,如果你觉得方便的话也可以在代码中使用它们. https://laravelacademy.org/post/8967.html ...
- Hadoop(17)-MapReduce框架原理-MapReduce流程,Shuffle机制,Partition分区
MapReduce工作流程 1.准备待处理文件 2.job提交前生成一个处理规划 3.将切片信息job.split,配置信息job.xml和我们自己写的jar包交给yarn 4.yarn根据切片规划计 ...
- 贪心算法之Huffman
Huffman编码,权重越大,离根节点越大.所以就是不断的选取两个最小的树,然后组成一颗新树,加入集合,然后去除已选的两棵树.不断的循环,直到最后的树的集合只剩下一棵,则构建完成,最后输出Huffma ...
- java入门---windows和Linux,UNIX,Solaris,FreeBSD下开发环境配置
首先来看Windows下的操作.我们需要下载java开发工具包JDK.下载地址:http://www.oracle.com/technetwork/java/javase/downloads/ ...
- Spark是什么
官方直达电梯 Spark一种基于内存的通用的实时大数据计算框架(作为MapReduce的另一个更优秀的可选的方案) 通用:Spark Core 用于离线计算,Spark SQL 用于交互式查询,Spa ...
- django 解决cors问题
首页 博客 学院 下载 GitChat TinyMind 论坛 问答 商城 VIP 活动 招聘 ITeye CSTO 写博客 发Chat 登录注册 AFei0018-博客 穷则思变,差则思勤.Pyth ...
- 听雷哥浅谈Redis
Linux下安装redis 1.下载源码,解压缩后编译源码. $ wget http://download.redis.io/releases/redis-2.8.3.tar.gz $ tar xzf ...
- CentOS修改网卡名称
转 一.问题说明 测试环境中出现的小问题,因为虚拟机之间经常复制来复制去,导致网卡配置这块的不一致现象. 配置文件的信息: [root@ora10g network-scripts]# catifcf ...
- gitk中文乱码问题处理
执行了 git config --global gui.encoding utf- 查看 %USERPROFILE%\.gitconfig 文件中也有 [gui] encoding = utf-8 在 ...
- jmeter常用的内置变量
1. vars API:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html vars.get(& ...