C. Mike and gcd problem
题意:
定义一种beautiful数列,满足gcd(b1,b2,···,bn) > 1.
现在给出一个数列A,有一种操作:对ai操作,使得a[i] = ap[i]-a[i+1],a[i+1] = a[i]+a[i+1]
问最少多少次操作使得题中满足beautiful。
思路:
数学题就多思考,有公式的情况下向公式思考。
根据操作的方法(a, b) ——- (a-b,a+b)——–(-2b,2a),这意味着两个数字一定能使得gcd = 2.
大胆猜测就是找使得A数列的gcd = 2 的最少操作数,事实如此,也可证明当不是2 的情况,很明显更复杂。
对于A数列中主要是操作奇数的数字,当两个相邻的数字是奇数的时候只需操作一次,优先贪心这一种情况,最后解决单个存在的奇数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
const int maxn = 1e5+10;
typedef long long LL;
LL gcd(LL a,LL b)
{
if(b == 0) return abs(a);
return gcd(b,a%b);
}
int n;
LL a[maxn];
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i = 1;i <= n; i++) {
scanf("%I64d",&a[i]);
}
LL gcd1 = 0;
for(int i = 1;i <= n; i++) {
gcd1 = gcd(gcd1,a[i]);
}
if(gcd1 > 1) {
printf("YES\n0\n");
return 0;
}
LL ans = 0;
for(int i = 1;i < n; i++)
if((a[i]&1) && (a[i+1]&1)) {
ans += 1;
a[i] = a[i+1] = 2;
}
for(int i = 1;i <= n; i++) {
if(a[i]&1) {
ans += 2;
}
}
printf("YES\n%I64d\n",ans);
return 0;
}