地址:http://codeforces.com/contest/660/problem/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
32 7 28

output
12 7 9 28

思路:互质是两个数的最大公约数为1,即gcd(a,b)=1;

  如果相邻两个数不是互质的话,插个1就好了(比赛时没想到1,插得是1—100内某一质数,(因为x<10^9,所以100内的质数绝对可以,因为乘积大于最大取值范围了)

  代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <queue>
 7 #include <stack>
 8 #include <map>
 9 #include <vector>
10
11 #define PI acos((double)-1)
12 #define E exp(double(1))
13 using namespace std;
14
15 int a[1010];
16 int b[5000];
17 int prime[17] = { 0,7,13,19,23,31,37,41,43,47,53,59,61,67,71,73,79 };
18
19 int is_coprime(int a, int b)
20 {
21     int t = 1;
22     while (t)
23     {
24         if (b == 1)
25             return 1;
26         t = a %b;
27         a = b;
28         b = t;
29     }
30     return 0;
31 }
32
33 int main(void)
34 {
35     int n, k;
36     while (scanf("%d", &n) == 1)
37     {
38         k = 1;
39         memset(b, 0, sizeof(b));
40         for (int i = 1; i <= n; i++)
41             scanf("%d", &a[i]);
42         for (int i = 1; i<n; i++)
43         {
44             if (is_coprime(a[i], a[i + 1]))
45             {
46                 b[k++] = a[i];
47             }
48             else
49             {
50                 b[k++] = a[i];
51                 for (int j = 1; j <= 16; j++)
52                     if (is_coprime(a[i], prime[j]) && is_coprime(a[i + 1], prime[j]))
53                     {
54                         b[k++] = prime[j];
55                         break;
56                     }
57             }
58         }
59         b[k] = a[n];
60         cout << k - n << endl;
61         for (int i = 1; i<k; i++)
62             printf("%d ", b[i]);
63         printf("%d\n", b[k]);
64     }
65     return 0;
66 }

View Code

转载于:https://www.cnblogs.com/weeping/p/5371872.html

Educational Codeforces Round 11A. Co-prime Array 数学相关推荐

  1. Educational Codeforces Round 88 (Rated for Div. 2) E(数学)

    Educational Codeforces Round 88 (Rated for Div. 2)E 题目大意: 给你n,k(1<=k<=n<=5e5),从1到n中选k个数组成一个 ...

  2. Educational Codeforces Round 74 (Rated for Div. 2)

    Educational Codeforces Round 74 (Rated for Div. 2) 原题地址 # 题目 分数 是否AC A Prime Subtraction 900 ✅ B Kil ...

  3. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  4. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

  5. Educational Codeforces Round 111 (Rated for Div. 2)

    Educational Codeforces Round 111 (Rated for Div. 2) 题号 题目 知识点 A Find The Array B Maximum Cost Deleti ...

  6. Educational Codeforces Round 86 (Rated for Div. 2) Apr/26/2020 22:35UTC+8

    Educational Codeforces Round 86 Rated for Div. 2 A. Road To Zero B. Binary Period(找最小周期) C. Yet Anot ...

  7. Educational Codeforces Round #136 (Rated for Div. 2) A~C

    A. Immobile Knight 题意: 给定一个 n×m 的棋盘,棋盘上有一个 "马",可以在一个方向上移动两个单元格,在垂直方向上移动一个单元格. 问:将其放在哪个位置,才 ...

  8. Educational Codeforces Round 138 (Rated for Div. 2)-赛后总结

    Dashboard - Educational Codeforces Round 138 (Rated for Div. 2) - Codeforces 总结一个教训就是不要心急,特别是对于一些题目, ...

  9. Educational Codeforces Round 100 (Rated for Div. 2)

    文章目录 Educational Codeforces Round 100 (Rated for Div. 2) A. Dungeon B. Find The Array C. Busy Robot ...

最新文章

  1. 福师2021计算机应用基础,2021福师《计算机应用基础》在线作业二【满分答案】...
  2. 构建安全的 ASP.NET 应用程序
  3. 剑指offer:和为S的两个数字
  4. Callable、Future阻塞队列阻塞栈
  5. 15-07-08 数组-- 手机号抽奖、福利彩票随机生成
  6. python怎么实现音乐快进,python将音频进行变速的操作方法
  7. OOP设计思考——何时使用接口?
  8. Linux命令行下关机【Ubuntu】
  9. Python实现文本自动分类(朴素贝叶斯方法)
  10. 2020-12-14(全局/静态对象的构造函数和析构函数调用的时机以及地址)
  11. RabbitMq--1
  12. Struts2 在页面定义变量 s:set标签
  13. vue使用echarts可视化图形插件
  14. Android网络请求开源框架retrofit的基本GET用法(2.4版本)
  15. 学以致用二十二-----写一个基本环境设置的脚本
  16. 如何测量代码运行时间
  17. EXCEL里常用的几个正则表达式
  18. 不限流量的物联卡是否真存在
  19. oracle+规则执行,Oraclejobinterval规则
  20. tomcat安装及配置教程(保姆级)

热门文章

  1. Document,Node,Element,HTMLDocument ,HTMLCollection,HTMLElement,NodeList
  2. 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
  3. golang网站错误处理
  4. static的应用和作用
  5. C++中的const
  6. lua源代码分析02:内存管理
  7. 银行系统开发必读的三本书!银行IT开发
  8. 常考数据结构与算法:求二叉树的层序遍历
  9. 面向对象,面向过程编程
  10. linux 安装centos