Avito Cool Challenge 2018 E. Missing Numbers 【枚举】
传送门:http://codeforces.com/contest/1081/problem/E
E. Missing Numbers
2 seconds
256 megabytes
standard input
standard output
Chouti is working on a strange math problem.
There was a sequence of nn positive integers x1,x2,…,xnx1,x2,…,xn, where nn is even. The sequence was very special, namely for every integer ttfrom 11 to nn, x1+x2+...+xtx1+x2+...+xt is a square of some integer number (that is, a perfect square).
Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. x2,x4,x6,…,xnx2,x4,x6,…,xn. The task for him is to restore the original sequence. Again, it's your turn to help him.
The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
The first line contains an even number nn (2≤n≤1052≤n≤105).
The second line contains n2n2 positive integers x2,x4,…,xnx2,x4,…,xn (1≤xi≤2⋅1051≤xi≤2⋅105).
If there are no possible sequence, print "No".
Otherwise, print "Yes" and then nn positive integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤10131≤xi≤1013), where x2,x4,…,xnx2,x4,…,xn should be same as in input data. If there are multiple answers, print any.
Note, that the limit for xixi is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying 1≤xi≤10131≤xi≤1013.
6
5 11 44
Yes
4 5 16 11 64 44
2
9900
Yes
100 9900
6
314 1592 6535
No
In the first example
- x1=4x1=4
- x1+x2=9x1+x2=9
- x1+x2+x3=25x1+x2+x3=25
- x1+x2+x3+x4=36x1+x2+x3+x4=36
- x1+x2+x3+x4+x5=100x1+x2+x3+x4+x5=100
- x1+x2+x3+x4+x5+x6=144x1+x2+x3+x4+x5+x6=144
All these numbers are perfect squares.
In the second example, x1=100x1=100, x1+x2=10000x1+x2=10000. They are all perfect squares. There're other answers possible. For example, x1=22500x1=22500 is another answer.
In the third example, it is possible to show, that no such sequence exists.
题意概括:
给出 N/2 个 偶数位的数,需要补充剩余 N/2 个奇数位的数,使得 每一位的前缀和都是完全平方数。
如果存在解,按顺序输出 N 个完整数字。
解题思路:
从第一位开始构造前缀和,如果按照实际的数的范围去枚举 x(1e13)肯定会爆炸,那么就折半枚举√x,判断当前前缀和 Si 是否为完全平方数,
如果是则记录下来,并且从 √Si + 1开始枚举求解下一个前缀和。
如果超过的 √Smax ,说明无解。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 1e5+;
const LL MAXW = ; LL b[MAXN], a[MAXN]; bool check(LL x)
{
LL y = sqrt(x);
if(y*y == x) return true;
return false;
} int main()
{
int N;
LL sum = ;
scanf("%d", &N);
for(int i = ; i <= N/; i++){
scanf("%I64d", &a[i]);
} LL tmp = 1LL;
while(!check(tmp*tmp+a[])){
tmp++;
if(tmp >= MAXW){
puts("No");
return ;
}
}
b[] = tmp*tmp;
b[] = a[];
int cnt = ;
tmp = sqrt(tmp*tmp+a[]); for(int i = ; i <= N/; i++){
if(tmp >= MAXW){
puts("No");
return ;
}
sum = tmp;
tmp++;
while(!check(tmp*tmp+a[i])){
tmp++;
if(tmp >= MAXW){
puts("No");
return ;
}
}
b[++cnt] = tmp*tmp - sum*sum;
b[++cnt] = a[i];
tmp = sqrt(tmp*tmp + a[i]);
printf("a:%I64d tmp:%I64d\n",a[i], tmp);
}
puts("Yes");
for(int i = ; i <= cnt; i++)
printf("%I64d ", b[i]); return ;
}
Avito Cool Challenge 2018 E. Missing Numbers 【枚举】的更多相关文章
- Avito Cool Challenge 2018 :E. Missing Numbers
E. Missing Numbers 题目链接:https://codeforces.com/contest/1081/problem/E 题意: 现在有n个数(n为偶数),但只给出a2,a4.... ...
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Avito Cool Challenge 2018
考挂了.. A - Definite Game 直接看代码吧. #include<cstdio> #include<cstring> #include<algorithm ...
- Avito Cool Challenge 2018(div1+2)
A. Definite Game: 题意:输入N,输出最小的结果N-x,其中x不少N的因子. 思路:N=2时,输出2:其他情况输出1:因为N>2时,N-1不会是N的因子. #include< ...
- Avito Cool Challenge 2018 Solution
A. Definite Game 签. #include <bits/stdc++.h> using namespace std; int main() { int a; while (s ...
- Avito Cool Challenge 2018 自闭记
A:n==2?2:1. #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...
- Avito Code Challenge 2018 C
C. Useful Decomposition time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Avito Code Challenge 2018 A~E
A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...
- cf掉分记——Avito Code Challenge 2018
再次作死的打了一次cf的修仙比赛感觉有点迷.. 还好掉的分不多(原本就太低没法掉了QAQ) 把会做的前三道水题记录在这.. A: Antipalindrome emmmm...直接暴力枚举 code: ...
随机推荐
- XmlSerialize
以前配置文件都直接写在TXT文件,能看懂就行: 后来写了点代码,就把配置写在ini文件里: 再后来随着趋势就把配置类序列化到本地,即xml配置: 现在懒了,直接ToJson到本地,需要时FromJso ...
- C#之RabbitMQ系列(一)
RabbitMQ–环境搭建 MQ MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接 ...
- XAMl使用其他命名空间中的类型及加载和编译
以前我们讲过XAMl命名空间.为了使便宜钱知道XAMl文档中元素对应的.NET类型,需要知道XAMl明档中指定特定的两个命名空间.XAML是一种实例化.NET对象的通用方法 ,除了可以实例化一些标准的 ...
- java实现mysql的备份还原
此文章是基于 1. 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 2. jquery+springMVC实现文件上传 一. 简介 备份和导入是一个互逆的过程. ...
- react相关知识点链接整理
1.React组件之间的通信 2.中间件做代理解决跨域问题 3.不要再问我跨域的问题了 4.React 组件数据流 && 组件间沟通 5.如何理解虚拟DOM 6.react性能调谐与d ...
- UNIX/Linux系统管理技术手册(1)----脚本和shell
1. 管道和重定向 (i) 要让第二条命令只有在第一条命令成功完成之后才执行,可以用一个 && 符号把两条命令隔开.例如: $ > /dev/null && cd ...
- Java中生成帮助文档
如何在Java中使用注释 在编写程序时,经常需要添加一些注释,用以描述某段代码的作用. 一般来说,对于一份规范的程序源代码而言,注释应该占到源代码的 1/3 以上.因此,注释是程序源代码的重要组成部分 ...
- 数组多功能splice()方法的插入,删除,替换
多功能splice()插入.删除.替换 <script type="text/javascript"> var arr=['A','B','C','D','E','F' ...
- 【转】ubunt 安装 yum出现 ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
今天本来是用xshell 链接本地的linux的,然而链接的时候没有主动弹出输入 用户名和密码的对话框,google搜了下,遇到了上面的问题. 解决办法如下:1.终端输入 ps aux ,列出进程. ...
- wpf 转型 android开发总结
今年马上就要过去了,这一年中我经历了从wpf转型到qt/qml,然后最近又要开始搞android,从我个人的经验来看不论是qml还是android从框架和设计上都跟wpf类似,并且移动端的设计因为很多 ...