题目大意:
  告诉你一个长度为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的更多相关文章

  1. CF763C Timofey and Remoduling

    题目戳这里. 这道题目纯粹是考思维. 若\(2N \le M\),由于答案肯定是\(s,s+d,\dots,s+(N-1)d\),我们任意枚举两个数\(a,b\),不妨设\(b\)在数列中出现在\(a ...

  2. Codeforces 763A. Timofey and a tree

    A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【codeforces 764B】Timofey and cubes

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 【leetcode 简单】 第三十五题 环形链表

    给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * struct ListNode { * ...

  2. Oracle笔记之表空间

    Oracle中有一个表空间的概念,一个数据库可以有好几个表空间,表放在表空间下. 1. 创建表空间 创建表空间使用create tablespace命令: CREATE TABLESPACE foo_ ...

  3. typeof运算符

    javascript中typeof用来判断一个变量或表达式的数据类型. typeof 返回值有六种可能: "number," "string," "b ...

  4. 解决爬虫时网站采用gb2312编码所遇到的乱码问题!

    import requests from bs4 import BeautifulSoupall_url = 'http://www.7160.com/qingchunmeinv/' # 请求头 he ...

  5. uboot1.1.6 start.s分析

    .Stage1 start.S代码结构 u-boot的stage1代码通常放在start.S文件中,他用汇编语言写成,其主要代码部分如下:(1)定义入口.由于一个可执行的Image必须有一个入口点,并 ...

  6. 福建工程学院寒假作业第一周G题

    涨姿势题1 TimeLimit:1000MS  MemoryLimit:128000KB 64-bit integer IO format:%lld   涨姿势题就是所谓的优化题,在组队赛中,队伍发现 ...

  7. springMVC中ajax的实现

    function addDebtResult(){ var repayIds=$("#repayIds").val(); var lateFeeDay=$("#repay ...

  8. Geoserver WFS跨域设置

    测试版本为geoserver2.11.0. 两种方法都可以实现跨域设置: 第一种: 下载跨域jar包jetty-servlets.jar(下载geoserver使用的对应jetty版本——可以查看&l ...

  9. jfinal文件上传

    jfianl获取表单数据,需要先getFile()获取文件,再使用getPara() public class ImageUploadController extends Controller{ pu ...

  10. 发布PHP项目(nginx+PHP7+mysql 5.6)

    一.环境检查 1.检查nginx ps -ef | grep "nginx" 显示如下内容则代表nginx启动正常 root 3285 1 0 12:57 ? 00:00:00 n ...