zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 11 A

    A. Co-prime Array
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 adding kelements to it.

    If there are multiple answers you can print any one of them.

    Example
    input
    3
    2 7 28
    output
    1
    2 7 9 28

     题意:增加最少的正整数  使得序列相邻的数都互质

     题解: 原序列中 若相邻的不互质 就把1放到中间 构成新序列

     1 #include<bits/stdc++.h>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<queue>
     6 #include<stack>
     7 #include<map> 
     8 #define ll __int64
     9 using namespace std;
    10 int n;
    11 int a[1005];
    12 map<int,int> mp;
    13 int ans;
    14 void init()
    15 {
    16     mp.clear();
    17     ans=0;
    18     for(int i=1;i<=n-1;i++)
    19     {
    20         if(__gcd(a[i],a[i+1])==1)
    21           {
    22               ans++;
    23           mp[i]=1;
    24           }
    25     }
    26 }
    27 int main()
    28 {
    29     
    30     scanf("%d",&n);
    31     for(int i=1;i<=n;i++)
    32         scanf("%d",&a[i]);
    33     init();
    34     printf("%d",n-1-ans); 
    35     printf("
    %d",a[1]);
    36     for(int i=2;i<=n;i++)
    37     {
    38      if(mp[i-1]==0)
    39     printf(" 1 %d",a[i]);
    40      else
    41      printf(" %d",a[i]);
    42     }
    43     cout<<endl;
    44     return 0; 
    45 } 
  • 相关阅读:
    leetcode38.外观数列(循环匹配)
    leetcode35.搜索插入位置(遍历并进行大小判断)
    leetcode28.实现strStr()(暴力拆解,双指针,KMP算法)
    JavaScript对象
    数组迭代
    数组的用法:
    数组
    while与do while 区别 for循环的简介及break和continue的区别
    for循环语句
    if.......else语句子
  • 原文地址:https://www.cnblogs.com/hsd-/p/5463895.html
Copyright © 2011-2022 走看看