codeforces 660A A. Co-prime Array(水题)

题目链接:

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 k elements 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

题意:

给一个数组,问插入多少个数字才能使相邻的两个数互质;

思路:

暴力找呗,找到的放队列里,最后输出不就好了;

AC代码:

/*
2014300227    660A - 7    GNU C++11    Accepted    15 ms    2188 KB
*/

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+4;
typedef long long ll;
const double PI=acos(-1.0);
int n,a[3000];
int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
}
queue<int>qu;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int num=0;
for(int i=1;i<n;i++)
{
qu.push(a[i]);
if(gcd(a[i],a[i+1])>1)
{

 
     

qu.push(1);
num++;
}
}
qu.push(a[n]);
printf("%d\n",num);
while(!qu.empty())
{
printf("%d ",qu.front());
qu.pop();
}

 
     

return 0;
}

 

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5370479.html

这道题是一道博弈论题目,需要使用到 SG 函数。SG 函数是一个函数,它的输入是一个状态,输出是一个非负整数,用来表示当前状态的取胜情况。具体而言,若 SG 函数的输出为 $0$,则当前状态必败;若 SG 函数的输出不为 $0$,则当前状态必胜。 对于这道题目,我们需要先了解一下如何通过数学分析求出 SG 函数。首先,我们需要对游戏的状态进行编码。对于本题,可以将状态表示为一个三元组 $(n, m, k)$,表示当前棋盘的大小为 $n \times m$,每个人每次最多可以取 $k$ 个棋子。接下来,我们需要定义一个从状态到状态集合的映射 $f(\cdot)$,表示从当前状态可以转移到哪些状态。对于本题,$f((n, m, k))$ 中的每个状态可以通过一次操作得到,即将当前棋盘中的某一行或某一列中的 $k$ 个棋子全部取走,然后将其剩余的部分作为新的棋盘状态。注意,对于这个映射,我们只需要考虑下一步能够到达的状态,而不需要考虑更远的状态。 接下来,我们需要定义 SG 函数的递归式。对于一个状态 $(n, m, k)$,其 SG 值可以通过其可以转移到的状态的 SG 值来计算。具体而言,我们可以将当前状态转移到的所有状态的 SG 值进行异或运算,并将结果加上 $1$,即 $SG((n, m, k)) = \text{mex}\{ SG(f((n, m, k))) \} + 1$,其中 $\text{mex}$ 表示集合中未出现的最小非负整数。 最后,我们需要解决的问题就是如何计算 $\text{mex}$ 函数。对于本题,可以使用一个 $O(k)$ 的算法来计算 $\text{mex}$。具体而言,我们可以记录所有出现的 SG 值,然后从 $0$ 开始枚举所有可能的非负整数,找到第一个未出现的整数即为 $\text{mex}$。 下面是 AC 代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值