Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】
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 every1 ≤ 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数列。让你再构造一个数列,使得该数列内gcd(ai, ai + 1, ..., aj) 都出现在S。
【分析】:如果最小元素不是给定集合的GCD,则答案为-1,否则,我们可以在集合的两个连续元素之间插入最小元素。序列长度为2n-1,满足约束条件。
//要求所有之间的gcd都在集合中,所以答案的所有元素的gcd必须在集合中。 此外,gcd(a,b)<= min(a,b),所以答案中所有元素的gcd必须是集合中最小的数字,所以每个数字必须将其分开。那么只需在原数列的相邻两个数ai,ai+1中插入原序列最小的数就这样可以保证gcd不是自己,就是最小的数。
【代码】:
#include <bits/stdc++.h> using namespace std;
typedef long long LL;
const int maxn = ; int a[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",a+i);
if(n==)
{
printf("1\n%d",a[]);
return ;
} int ok=; for(int i=;i<=n;i++)
if(a[i]%a[]!=)
{
ok=;
break;
}
//2 3
if(!ok)//如果最小元素不是给定集合的GCD,则答案为-1
{
printf("-1");
return ;
} printf("%d\n",(n-)*);//否则,我们可以在集合的两个连续元素之间插入最小元素。序列长度为2n-1,满足约束条件。
for(int i=;i<=n;i++) printf("%d %d ",a[],a[i]);
return ;
}
数学构造
Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】的更多相关文章
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #447 (Div. 2) 题解 【ABCDE】
BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...
- Codeforces Round #447 (Div. 2)
我感觉这场CF还是比较毒的,虽然我上分了... Problem A QAQ 题目大意:给你一个由小写字母构成的字符串,问你里面有多少个QAQ. 思路:找字符串中的A然后找两边的Q即可,可以枚举找Q, ...
- 【Codeforces Round #447 (Div. 2) C】Marco and GCD Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把gcd(a[1..n])放在输入的n个数之间. [代码] /* 1.Shoud it use long long ? 2.Have ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学
题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...
- Codeforces Round #447 (Div. 2) 题解
A.很水的题目,3个for循环就可以了 #include <iostream> #include <cstdio> #include <cstring> using ...
- Codeforces Round #447 (Div. 2) C 构造
现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】
A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
随机推荐
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- Asp.Net中Response.Cookies.Remove 无法删除COOKIE的问题解决方法
登陆功能经常需要使用Cookie来存储登陆信息,可是在开发过程中,经常发现cookie无法删除的问题.删除的代码无非就是找到Cookie并删除掉. 但是会发现 Response.Cookies.Rem ...
- 孤荷凌寒自学python第四十一天python的线程同步之Event对象
孤荷凌寒自学python第四十一天python的线程同步之Event对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 鉴于Lock锁与RLock锁均宣告没有完全完成同步文件操作的问题,于 ...
- java_链表反转
定义一个Node节点类 1 public class Node { 2 public int value; 3 public Node next; 4 5 public Node(int value) ...
- Set(), Get() 真正的目的
在各种面向对象编程中,都有 Set(), Get() 两种方法. 1 常见理解 1 为了保证安全性 2 为了规范代码 其实这些理解都是对的.具体看我们从哪个角度去理解这个内容. 2 个人理解 2.1 ...
- 课时1:我和python的第一次亲密接触
目录: 一.Python3的下载与安装 二.从IDIE启动Python 三.尝试点新的东西 四.为什么会这样? 五.课时01课后习题及答案 ============================== ...
- React03 移动端跨平台开发
目录 React-day03 RN移动端开发 了解React-Native 了解React-Native工作流程 创建第一个React-Native项目 * 了解React-Native项目及结构 开 ...
- 原始套接字--arp相关
arp请求示例 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <un ...
- mysql安装目录、配置文件存放位置
linux系统下,如何知道mysql使用的配置文件到底是哪个呢?linux自带的mysql的安装目录又是什么呢?数据存放在什么目录下? 1.linux系统自带的mysql,其安装目录及数据目录查看方法 ...
- Oracle常用数据库系统表单以及SQL的整理
因为最近涉及到了一些数据库的归档,备份等工作,所以一部分的重心放在了数据库上,毕竟之前对数据库的了解也只停留在了一般的建表,查询,最多最多再写一写触发器之类的东西. 通常都是自己瞎搞搞,也就懂一点皮毛 ...