Codeforces 894.C Marco and GCD Sequence
1 second
256 megabytes
standard input
standard output
In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.
When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.
Note that even if a number is put into the set S twice or more, it only appears once in the set.
Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.
The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S.
The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.
If there is no solution, print a single line containing -1.
Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000.
In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence.
We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106.
If there are multiple solutions, print any of them.
4
2 4 6 12
3
4 6 12
2
2 3
-1
In the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.
大致题意:给定一个序列S,由原序列任意一对(i,j)组成的区间[i,j]中的数的gcd构成,S中没有重复的数,求原序列.
分析:这种题没啥意思.i可以等于j,那么一种想法就是把所有的数放在原序列中,这样的话可能有些连续的数的gcd会出现S中没有出现过的数,解决方法也很简单,在每两个数中间插入一个s[1]即可,因为s[1]是最小的gcd,肯定是所有数的约数.据此可以判断是否有解.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <algorithm> using namespace std; int n, a[];
bool flag = false; int main()
{
scanf("%d", &n);
scanf("%d", &a[]);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] % a[] != )
flag = ;
}
if (flag)
printf("-1\n");
else
{
printf("%d\n", n * );
for (int i = ; i <= n; i++)
printf("%d %d ", a[i], a[]);
} return ;
}
Codeforces 894.C Marco and GCD Sequence的更多相关文章
- codeforces #447 894A QAQ 894B Ralph And His Magic Field 894C Marco and GCD Sequence
A.QAQ 题目大意:从给定的字符串中找出QAQ的个数,三个字母的位置可以不连续 思路:暴力求解,先找到A的位置,往前扫,往后扫寻找Q的个数q1,q2,然 后相乘得到q1*q2,这就是这个A能够找到的 ...
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】
C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...
- codeforces 894C - Marco and GCD Sequence - [有关gcd数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894C In a dream Marco met an elderly man with a pair o ...
- 【Codeforces Round #447 (Div. 2) C】Marco and GCD Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把gcd(a[1..n])放在输入的n个数之间. [代码] /* 1.Shoud it use long long ? 2.Have ...
- CF894C Marco and GCD Sequence
题目链接:http://codeforces.com/contest/894/problem/C 题目大意: 按照严格递增的顺序给出 \(m\) 个数作为公因数集,请你构造出一个数列,对于数列中的任意 ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)
C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...
随机推荐
- flex布局与ellipsis冲突问题
在flex布局里使用text-overflow: ellipsis;发现没有省略. 解决方案 .g-flex-c{ flex: 1; min-width: 0; }
- MySQL-MMM方案
参考文档: 官方文档:http://mysql-mmm.org/mmm2:guide 本文对mmm方案做简单介绍,并做1个简单的验证. 一.MySQL-MMM方案 1. MMM方案简介 MMM(Mul ...
- 论文笔记:Visual Object Tracking based on Adaptive Siamese and Motion Estimation Network
Visual Object Tracking based on Adaptive Siamese and Motion Estimation 本文提出一种利用上一帧目标位置坐标,在本帧中找出目标可能出 ...
- java-HttpGetPost-图片字节流上传
在java程序开发中经常用到与服务端的交互工作,主要的就是传递相应的参数请求从而获取到对应的结果加以处理 可以使用Get请求与Post请求,注意!这里的Get请求不是通过浏览器界面而是在程序代码中设置 ...
- Linux学习—导航文件系统
与windows相同,Linux操作系统也是以被称之为分层目录结构的方式来组织文件的.这意味着文件是以树形结构的目录中进行组织的,该树形结构目录可能包含文件和其他目录.文件系统的第一个目录叫做根目录, ...
- OpenCV学习笔记——imread、imwrite以及imshow
1.imread Loads an image from a file. 从文件中读取图像. C++: Mat imread(const string& filename, int flags ...
- Controller与Switch建立连接
连接建立 控制器和交换机认识的过程. 用于交互Openflow版本,如果不同则没有后续. 同1. 特征请求,控制器询问交换机的特征信息. 交换机回复控制器,相当于把整个交换机的所有配置都告诉控制器了. ...
- SGU 126 Boxes(模拟题|二进制)
Translate:Sgu/126 126. 盒子 time limit per test: 0.5 sec. memory limit per test: 4096 KB 有两个盒子. 第一个盒子里 ...
- java 抽象类&接口
1,抽象类中有构造函数吗? 有,用于给子类对象进行初始化. 2,抽象关键字不可以和那些关键字共存? private 不行 static 不行 final 不行 final关键字: 1,fina ...
- Charles安装及使用教程
一. 简介及安装 一.charles的使用 1.1 charles的说明 Charles其实是一款代理服务器,通过过将自己设置成系统(电脑或者浏览器)的网络访问代理服务器,然后截取请求和请求结果达到 ...