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 ...
随机推荐
- 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字
剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...
- iOS笔记056 - UI总结02
九宫格布局 UICollectionViewController 创建控制器一定要指定默认的布局样式. // 加载一个九宫格布局的控制器,必须指定布局样式 UICollectionViewFlowLa ...
- 4.实现简单的shell sed替换功能
# -*- coding:utf-8 -*- # Author: JACK ZHAO # 程序1: 实现简单的shell sed替换功能 import sys #判断参数个数 if len(sys.a ...
- 新建git仓库--留
1.git config 配置配置息,查看配置信息
- 【志银】MySQL命令总结
===0-MySQL密码设置===0.1-登入和进入MySQL数据库: 0.1.1-登入MySQL数据库:C:\Users\Administrator>mysql -u用户名 -hMySQL服务 ...
- UTXO是什么?
以易于理解的方式解释了比特币交易中的"UTXO" UTXO 2017年11月1日 让我们看看当你发一点硬币时会发生什么. 比特币交易通过UTXO执行.通过在比特硬币的所有交易中新生 ...
- Alpha 冲刺(5/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...
- quagga源码学习--BGP协议中的routemap
路由策略的基础知识 定义 路由策略(Routing Policy)作用于路由,主要实现了路由过滤和路由属性设置等功能,它通过改变路由属性(包括可达性)来改变网络流量所经过的路径. 目的 路由器在发布. ...
- poj2388 更水
Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34974 Accepted: 2 ...
- 《c程序设计语言》读书笔记-4.2-扩充atof函数
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...