暑假第一周 C . CodeForces 660A Co-prime Array
2017-07-02 11:37
302 查看
题意:给你一个序列,让你插入最小的数,使得任意相邻的两个数都互质,让你输出最后的序列长什么样
思路:显然插入的数最优就是1嘛,1和任何数都互质,然后我们贪心的去插就好了,能插就插
Description
You are given an array of n elements, you must make it a co-prime array in as few moves as possible.
In each move you can insert any positive integral number you want not greater than 109 in any place in the array.
An array is co-prime if any two adjacent numbers of it are co-prime.
In the number theory, two integers a and b are said to be co-prime if the only positive integer that divides both of them is 1.
Input
The first line contains integer n (1 ≤ n ≤ 1000) — the number of elements in the given array.
The second line contains n integers ai (1 ≤ ai ≤ 109)
— the elements of the array a.
Output
Print integer k on the first line — the least number of elements needed to add to the array a to make it co-prime.
The second line should contain n + k integers aj — the elements of the array a after
adding k elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array a by
addingk elements to it.
If there are multiple answers you can print any one of them.
Sample Input
Input
Output
//C********
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int a[maxn];
int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
vector<pair<int,int> >P;
int main()
{
int n,i,j=0;
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
}
for(i=1;i<n;i++){
if(gcd(a[i],a[i+1])!=1)
P.push_back(make_pair(1,i));
}
cout<<P.size()<<endl;
for(i=1;i<=n;i++){
cout<<a[i]<<" ";
if(j<P.size()&&P[j].second==i){
cout<<P[j].first<<" ";
j++;
}
}
cout<<endl;
}
思路:显然插入的数最优就是1嘛,1和任何数都互质,然后我们贪心的去插就好了,能插就插
Description
You are given an array of n elements, you must make it a co-prime array in as few moves as possible.
In each move you can insert any positive integral number you want not greater than 109 in any place in the array.
An array is co-prime if any two adjacent numbers of it are co-prime.
In the number theory, two integers a and b are said to be co-prime if the only positive integer that divides both of them is 1.
Input
The first line contains integer n (1 ≤ n ≤ 1000) — the number of elements in the given array.
The second line contains n integers ai (1 ≤ ai ≤ 109)
— the elements of the array a.
Output
Print integer k on the first line — the least number of elements needed to add to the array a to make it co-prime.
The second line should contain n + k integers aj — the elements of the array a after
adding k elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array a by
addingk elements to it.
If there are multiple answers you can print any one of them.
Sample Input
Input
3 2 7 28
Output
1 2 7 9 28
//C********
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int a[maxn];
int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
vector<pair<int,int> >P;
int main()
{
int n,i,j=0;
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
}
for(i=1;i<n;i++){
if(gcd(a[i],a[i+1])!=1)
P.push_back(make_pair(1,i));
}
cout<<P.size()<<endl;
for(i=1;i<=n;i++){
cout<<a[i]<<" ";
if(j<P.size()&&P[j].second==i){
cout<<P[j].first<<" ";
j++;
}
}
cout<<endl;
}
相关文章推荐
- Codeforces 660A Co-prime Array 【水题】
- CodeForces 660A Co-prime Array
- CodeForces 660A Co-prime Array
- CodeForces - 660A Co-prime Array (模拟)
- CodeForces 660A Co-prime Array(互质数列)
- CodeForces 660A Co-prime Array
- CodeForces 660A Co-prime Array
- 暑假第一周 D CodeForces 660 B.
- CodeForces 660A Co-prime Array
- CSU-ACM2017暑假集训比赛7 A - Equivalent Strings - CodeForces - 560D
- codeforces 660A Co-prime Array
- 【codeforces】-660Co-prime Array
- 暑假集训大二第一周周三赛 A仙人掌的残影
- 【CodeForces】660A - Co-prime Array(水)
- 暑假第一周 J hdu 5702 (结构体排序)
- Codeforces 660A:Co-prime Array(水题+思维)
- 暑假的第一周
- CodeForces 597A Divisibility 在区间被整除的个数 暑假小练习R
- 北京大学暑假学校ACM/ICPC课程第一周总结
- 暑假第一周 I