Educational Codeforces Round 11A. Co-prime Array 数学
地址: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 outputYou 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.
InputThe 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.
OutputPrint 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 input3output
2 7 28
1
2 7 9 28
思路:互质是两个数的最大公约数为1,即gcd(a,b)=1;
如果相邻两个数不是互质的话,插个1就好了(比赛时没想到1,插得是1—100内某一质数,(因为x<10^9,所以100内的质数绝对可以,因为乘积大于最大取值范围了)
代码:
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <queue> #include <stack> #include <map> #include <vector> #define PI acos((double)-1) #define E exp(double(1)) using namespace std; int a[1010]; int b[5000]; int prime[17] = { 0,7,13,19,23,31,37,41,43,47,53,59,61,67,71,73,79 }; int is_coprime(int a, int b) { int t = 1; while (t) { if (b == 1) return 1; t = a %b; a = b; b = t; } return 0; } int main(void) { int n, k; while (scanf("%d", &n) == 1) { k = 1; memset(b, 0, sizeof(b)); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i<n; i++) { if (is_coprime(a[i], a[i + 1])) { b[k++] = a[i]; } else { b[k++] = a[i]; for (int j = 1; j <= 16; j++) if (is_coprime(a[i], prime[j]) && is_coprime(a[i + 1], prime[j])) { b[k++] = prime[j]; break; } } } b[k] = a ; cout << k - n << endl; for (int i = 1; i<k; i++) printf("%d ", b[i]); printf("%d\n", b[k]); } return 0; }View Code
- Android 蓝牙开发介绍
- 房屋建筑和市政基础设施工程竣工验收备案管理办法
- powerDesigner设置name不自动等于code
- unity再战PBR材质流程与材质制作实践
- TabLayout+ViewPager+Fragment完成滑动界面
- java 递归算法
- ZOJ2314 Reactor Cooling(无源汇流量有上下界网络的可行流)
- 推荐:移动端前端UI库—Frozen UI、WeUI、SUI Mobile
- Struts2源码分析(二)Struts2运行流程分析
- tcp详解学习笔记-tcp建立终止
- 银行家算法
- 手把手教你配置苹果APNS推送服务
- HDU2042
- Java中super的几种用法并与this的区别
- 算法初级_3 :函数与递归
- struts整合之 spring 对于 destroy listen等问题
- tcp链接端口的理解
- poj3253切割木板(哈夫曼树) 贪心【优先队列】
- system.currentTimeMillis() 获得当前的时间
- 实时操作系统