codeforces 660A A. Co-prime Array(水题)
2016-04-09 01:03
323 查看
题目链接:
A.Co-primeArray
timelimitpertest
1second
memorylimitpertest
256megabytes
input
standardinput
output
standardoutput
Youaregivenanarrayofnelements,youmustmakeitaco-primearrayinasfewmovesaspossible.
Ineachmoveyoucaninsertanypositiveintegralnumberyouwantnotgreaterthan109inanyplaceinthearray.
Anarrayisco-primeifanytwoadjacentnumbersofitareco-prime.
Inthenumbertheory,twointegersaandbaresaidtobeco-primeiftheonlypositiveintegerthatdividesbothofthemis1.
Input
Thefirstlinecontainsintegern(1 ≤ n ≤ 1000)—thenumberofelementsinthegivenarray.
Thesecondlinecontainsnintegersai(1 ≤ ai ≤ 109)—theelementsofthearraya.
Output
Printintegerkonthefirstline—theleastnumberofelementsneededtoaddtothearrayatomakeitco-prime.
Thesecondlineshouldcontainn + kintegersaj—theelementsofthearrayaafteraddingkelementstoit.Notethatthenewarrayshouldbeco-prime,soanytwoadjacentvaluesshouldbeco-prime.Alsothenewarrayshouldbegotfromtheoriginalarrayabyaddingkelementstoit.
Iftherearemultipleanswersyoucanprintanyoneofthem.
Example
input
output
#include<bits/stdc++.h>
usingnamespacestd;
constintN=1e5+4;
typedeflonglongll;
constdoublePI=acos(-1.0);
intn,a[3000];
intgcd(intx,inty)
{
if(y==0)returnx;
returngcd(y,x%y);
}
queue<int>qu;
intmain()
{
scanf("%d",&n);
for(inti=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
intnum=0;
for(inti=1;i<n;i++)
{
qu.push(a[i]);
if(gcd(a[i],a[i+1])>1)
{
qu.push(1);
num++;
}
}
qu.push(a
);
printf("%d\n",num);
while(!qu.empty())
{
printf("%d",qu.front());
qu.pop();
}
return0;
}
timelimitpertest
1second
memorylimitpertest
256megabytes
input
standardinput
output
standardoutput
Youaregivenanarrayofnelements,youmustmakeitaco-primearrayinasfewmovesaspossible.
Ineachmoveyoucaninsertanypositiveintegralnumberyouwantnotgreaterthan109inanyplaceinthearray.
Anarrayisco-primeifanytwoadjacentnumbersofitareco-prime.
Inthenumbertheory,twointegersaandbaresaidtobeco-primeiftheonlypositiveintegerthatdividesbothofthemis1.
Input
Thefirstlinecontainsintegern(1 ≤ n ≤ 1000)—thenumberofelementsinthegivenarray.
Thesecondlinecontainsnintegersai(1 ≤ ai ≤ 109)—theelementsofthearraya.
Output
Printintegerkonthefirstline—theleastnumberofelementsneededtoaddtothearrayatomakeitco-prime.
Thesecondlineshouldcontainn + kintegersaj—theelementsofthearrayaafteraddingkelementstoit.Notethatthenewarrayshouldbeco-prime,soanytwoadjacentvaluesshouldbeco-prime.Alsothenewarrayshouldbegotfromtheoriginalarrayabyaddingkelementstoit.
Iftherearemultipleanswersyoucanprintanyoneofthem.
Example
input
3 2728
output
1 27928 题意: 给一个数组,问插入多少个数字才能使相邻的两个数互质; 思路: 暴力找呗,找到的放队列里,最后输出不就好了; AC代码:
/* 2014300227660A-7GNUC++11Accepted15ms2188KB */
#include<bits/stdc++.h>
usingnamespacestd;
constintN=1e5+4;
typedeflonglongll;
constdoublePI=acos(-1.0);
intn,a[3000];
intgcd(intx,inty)
{
if(y==0)returnx;
returngcd(y,x%y);
}
queue<int>qu;
intmain()
{
scanf("%d",&n);
for(inti=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
intnum=0;
for(inti=1;i<n;i++)
{
qu.push(a[i]);
if(gcd(a[i],a[i+1])>1)
{
qu.push(1);
num++;
}
}
qu.push(a
);
printf("%d\n",num);
while(!qu.empty())
{
printf("%d",qu.front());
qu.pop();
}
return0;
}
相关文章推荐
- python中的字典
- 美团笔试题 最大差值
- wordpress the_date 方法 偶尔为空的问题
- ArcEngine属性条件查询和空间条件查询
- ==的小结
- 【经典DP】最大连续子序列和
- ffmpeg合并多个视频
- 软考六:网络与多媒体基础知识
- Windows下安装Resin配置
- STM32F103的4*4矩阵按键,调试通过
- FeatureLayer,FeatureDataset,FeatureClass,Feature,IFeatureCursor几个概念总结
- 基于AttenceTimelineCell的时间轴
- #6JAVA异常处理方案#
- C++常见的几个库函数详解
- .net源码分析 – List<T>
- 服务器开发需要的知识提纲
- LeetCode 137. Single Number II
- libnet windows下编译,出错与解决办法
- 覆盖Object类的equals、hashCode和toString方法
- c++第三次上机作业-计算分段函数