[2016-04-09][codeforces][660][A][ Co-prime Array]

  • 时间:2016-04-09 22:50:56 星期六

  • 题目编号:[2016-04-09][codeforces][660][A][ Co-prime Array]

  • 题目大意:给定一个数列,问至少需要插入多少个1 109中的任一数字,才能使得相邻两个数字是互质的,输出最少次数和最后的数列

  • 分析:直接扫一遍,相邻元素不互质的,中间插个1,

  1. #include<cstdio>
  2. #include<vector>
  3. using namespace std;
  4. const int maxn = 1000 + 10;
  5. vector<int> a;
  6. int gcd(int a,int b){
  7. return b == 0?a:gcd(b,a%b);
  8. }
  9. int main(){
  10. int n,tmp;
  11. scanf("%d",&n);
  12. for(int i = 0 ; i < n ; ++i){
  13. scanf("%d",&tmp);
  14. a.emplace_back(tmp);
  15. }
  16. int cnt = 0;
  17. vector<int>::iterator itv;
  18. for(itv = a.begin() + 1;itv != a.end();++itv){
  19. if(gcd(*itv,*(itv - 1)) != 1){
  20. itv = a.emplace(itv,1);
  21. ++cnt;
  22. }
  23. }
  24. printf("%d\n",cnt);
  25. for(itv = a.begin();itv != a.end();++itv){
  26. printf("%d ",*itv);
  27. }
  28. return 0;
  29. }


来自为知笔记(Wiz)


posted on 2016-04-09 23:27  红洋  阅读(151)  评论(0)    收藏  举报

导航