CF660A Co-prime Array
题意
给定 和一个 个元素的序列 ,可以在任意一个 中间插入一个数 ,其中 。现在要尽量插入较少的次数,使得任意 。
分析
先给个质数表。
明显,一个质数 只有 和 两个因数,所以对于任意一个 ,,而与 不互质的数有 ,那么我们只需要在一个 中间插入一个大质数,例如 ,而 。
但是有一种会被 hack,假设 中有 ,那么就不行,那么就输出 即可。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int a[N];
int main()
{
int n, cnt = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 2; i <= n; i++)
{
cnt += (__gcd(a[i], a[i - 1]) != 1);
}
printf("%d\n", cnt);
for (int i = 1; i < n; i++)
{
printf("%d ", a[i]);
if (__gcd(a[i], a[i + 1]) != 1)
{
if (a[i] == 917120411) printf("1 ");
else printf("917120411 ");
}
}
printf("%d ", a[n]);
return 0;
}

浙公网安备 33010602011771号