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

    题意:给出n个数,在其中插入一些数,使得任意两个相邻的数互质。
    题解:1和任意一个数互质,所以当找到两个数不满足条件时,在这之间插入一个1即可。
     1 /*A*/
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 const int maxn=1000+100;    
     7 int gcd(int a,int b)
     8 {
     9     return b==0?a:gcd(b,a%b);
    10 }
    11 
    12 int main()
    13 {
    14     int n;
    15     int a[maxn];
    16     int insert[maxn];
    17     while(scanf("%d",&n)!=EOF)
    18     {
    19         memset(a,0,sizeof(a));
    20         memset(insert,0,sizeof(insert));
    21         for(int i=0;i<n;i++)
    22             scanf("%d",&a[i]);
    23         int k=0;
    24         for(int i=0;i<n-1;i++)
    25         {
    26             int t=gcd(a[i],a[i+1]);
    27             if(t!=1)
    28             {
    29                 //insert[i]=getinsert(a[i],a[i+1]);
    30                 insert[i]=1;
    31                 k++;
    32             }    
    33         }
    34         printf("%d
    %d",k,a[0]);
    35         if(insert[0])
    36                 printf(" %d",insert[0]);
    37         for(int i=1;i<n;i++)
    38         {
    39             printf(" %d",a[i]);
    40             if(insert[i])
    41                 printf(" %d",insert[i]);
    42         }
    43         printf("
    ");
    44     }
    45     return 0;
    46 }
    View Code


  • 相关阅读:
    python基础:映射和集合类型
    python基础:列表生成式和生成器
    python基础:名称空间与作用域
    http://www.cnblogs.com/fczjuever/archive/2013/04/05/3000680.html
    2016新年总结
    send()和recv()函数详解
    python基础:测量python代码的运行时间
    python函数与方法装饰器
    Python基础:11.2_函数调用
    MySQL学习笔记-数据库后台线程
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5380103.html
Copyright © 2011-2022 走看看