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


  • 相关阅读:
    systabcontrol32
    winform 进程唯一,打开第二个激活第一个进程的窗体显示
    winform在 Xp下杀死进程
    安装包创建桌面快捷方式
    [最短路/线段树大法优化DIJ] 【模板】单源最短路径(标准版)
    [线段树模板题] 线段树2
    [线段树优化应用] 数星星Star
    [倍增思想/变种最短路] 跑路
    [DP/变种背包] SOFTWARE
    [前缀和/数论] 数列
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5380103.html
Copyright © 2011-2022 走看看