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 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.
Input
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.
Output
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.
Example
4
2 4 6 12
3
4 6 12
2
2 3
-1
Note
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.
题意:
有一个数组a[1~n],对他们所有的1<=i<=j<=n求 gcd( a[i] ~ a[j] ),得到集合S;
该集合S满足:元素不重复、集合内元素满足严格单增;
现在给你一个S,让你求出a;
题解:
gcd( a[1] ~ a[n] )显然是所有gcd( a[i] ~ a[j] )里最小的且满足 gcd( a[1] ~ a[n] ) | ∀gcd( a[i] ~ a[j] ),所以在集合S中S[1]应该满足 S[1] | S[i] ;
然后另外一个性质是gcd(num) = num,所以所有的a[i]都应该出现在S里;
我们当然不能像题目里样例那样求a[1~n],这样有点难,考虑另外的方法;
考虑让每个gcd(a[i])=S[i],然后让gcd(a[i]~a[j])=S[1](i<j),怎么操作呢,在S[2]~S[m]之间都插入S[1]即可。
AC代码:
#include <bits/stdc++.h>
using namespace std; int m,S[];
int main()
{
cin>>m;
for(int i=;i<=m;i++) scanf("%d",&S[i]); bool ok=;
for(int i=;i<=m;i++)
{
if(S[i]%S[]!=)
{
ok=;
break;
}
}
if(!ok)
{
printf("-1\n");
return ;
} printf("%d\n", m + ( (m-==)?():(m-) ) );
printf("%d ",S[]);
for(int i=;i<=m;i++)
{
if(i!=) printf(" %d ",S[]);
printf("%d",S[i]);
}
cout<<endl;
}
codeforces 894C - Marco and GCD Sequence - [有关gcd数学题]的更多相关文章
- 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 894.C Marco and GCD Sequence
C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 ...
- CF894C Marco and GCD Sequence
题目链接:http://codeforces.com/contest/894/problem/C 题目大意: 按照严格递增的顺序给出 \(m\) 个数作为公因数集,请你构造出一个数列,对于数列中的任意 ...
- Codeforces Round #554 (Div. 2)-C(gcd应用)
题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...
- Codeforces Round #651 (Div. 2) A. Maximum GCD(数论)
题目链接:https://codeforces.com/contest/1370/problem/A 题意 有 $n$ 个数大小分别为 $1$ 到 $n$,找出两个数间最大的 $gcd$ . 题解 若 ...
- Codeforces Round #554 (Div. 2) C. Neko does Maths (数论 GCD(a,b) = GCD(a,b-a))
传送门 •题意 给出两个正整数 a,b: 求解 k ,使得 LCM(a+k,b+k) 最小,如果有多个 k 使得 LCM() 最小,输出最小的k: •思路 时隔很久,又重新做这个题 温故果然可以知新❤ ...
- Codeforces Round #691 (Div. 2) C. Row GCD (数学)
题意:给你两个数组\(a\)和\(b\),对于\(j=1,...,m\),找出\(a_1+b_j,...,a_n+b_j\)的\(gcd\). 题解:我们很容易的得出\(gcd\)的一个性质:\(gc ...
- 欧几里得算法:从证明等式gcd(m, n) = gcd(n, m mod n)对每一对正整数m, n都成立说开去
写诗或者写程序的时候,我们经常要跟欧几里得算法打交道.然而有没要考虑到为什么欧几里得算法是有效且高效的,一些偏激(好吧,请允许我用这个带有浓重个人情感色彩的词汇)的计算机科学家认为,除非程序的正确性在 ...
随机推荐
- ios利用Reachability确认网络环境3G/WIFI(转)
iPhone开发技巧之网络篇(4)--- 确认网络环境 开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息.如果没有处理它们,是不会通过Apple的审查的. Apple 的 例程 Re ...
- Java查看内部类信息
Java中支持在类的内部定义类,这种类成为内部类.内部类有些像Java中的方法,可以使用访问权限限定符修饰,可以使用static修饰等.编写程序,利用Java的反射机制来查看内部类的信息. 思路分析: ...
- mongodb 搭建主从服务器
mongodb 主从配置比较简单,只需要在启动的时候添加参数(-master.-slave -source IP:PORT). Ubuntu 16.04 系统环境 监听端口分别为:27010.2701 ...
- WebService之CXF
一.配置环境变量(Windows系统下要重启) 1.JAVA_HOME即JDK安装路径bin上一级,java -version命令验证 2.CXF_HOME即cxf安装路径bin上一级,cxf解压包下 ...
- PHP代码审计笔记--CSRF漏洞
0x01 前言 CSRF(Cross-site request forgery)跨站请求伪造.攻击者盗用了你的身份,以你的名义向第三方网站发送恶意请求,对服务器来说这个请求是完全合法的,但是却完成了攻 ...
- java的两种冒泡算法
所谓的冒泡算法,就是给数组进行排序,可以根据以小到大的顺序,也可以根据以小到大的顺序,在数组的封装类java.util.Arrays通过sort方法进行按升序的排序.那不用类的话怎么进行呢? 思路一: ...
- 原创:Eclipse安装Eclipse Color Themes插件后,编辑器背景颜色被改变
如题,卸载Eclipse Color Themes插件后,背景颜色还是白色,蛋疼,修改.metadata\.plugins\org.eclipse.core.runtime\.settings中的or ...
- Hadoop学习笔记(1):WordCount程序的实现与总结
开篇语: 这几天开始学习Hadoop,花费了整整一天终于把伪分布式给搭好了,激动之情无法言表······ 搭好环境之后,按着书本的代码,实现了这个被誉为Hadoop中的HelloWorld的程序--W ...
- Android设计和开发系列第二篇:Action Bar(Develop—API Guides)
Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an ...
- css笔记 - transition学习笔记(二)
开始把7,8月份学的css整理一下 transition过渡 1. CSS transition transition过渡 :用于当元素 从一种样式变换为另一种样式 时为元素添加效果. 2. tran ...