codeforces 789C

题解:

首先计算数组整体的gcd,如果大于1,那么输出"YES"和"0"

接下来,考虑两个数x,y

令d=gcd(x-y,x+y),那么即存在p,q(gcd(p,q)=1)使得 x-y=pd , x+y=qd

那么有(p+q)d=2x,(q-p)d=2y,也就是说d|2x,d|2y

  gcd(x-y,x+y) | gcd(2x,2y)=2gcd(x,y)

说明每次move操作只能使得两个数的公因数乘以1或者乘以2

  若相邻两个数同为奇数或同为偶数,那么move后公因数乘以2

  若相邻两个数为一奇一偶,那么move后公因数不变

考虑全部N个数的话,显然一次move操作同样也只能让他们总体的公因数乘以1或者乘以2

问题化为了求最少的move次数使得他们全体为偶数

接下来问题就简单了0v0

若相邻的两个数为奇数,那么需要1次move操作让他俩都化为偶数

若相邻的两个数为偶数,那么需要0次move操作让他俩都化为偶数(没毛病)

若相邻的两个数为一奇一偶,那么需要2次move操作让他俩都化为偶数

接下来贪心就好了,扫两次,第一次贪奇数对的个数,过两个更两个,第二次贪一奇一偶的个数

http://www.cnblogs.com/scidylanpno/p/6753960.html


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 100000 + 10;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;
#define eps 10e-10
const ll Mod = 1000000007;
typedef pair<ll, ll> P;
int a[maxn];
int gcd(int x,int y)
{
    return y ? gcd(y,x % y) : x;
}
int main()
{
    int n;
    while( ~ scanf("%d",&n))
    {
        for(int i = 1; i <= n; i ++)
            scanf("%d",&a[i]);
        int t = abs(a[1]);
        a[1] = a[1] & 1;
        for(int i = 2; i <= n; i ++)
            t = gcd(t,abs(a[i])),a[i] &= 1;
        if(t > 1)
        {
            puts("YES");
            puts("0");
            continue;
        }
        int ans = 0;
        for(int i = 1; i < n; i ++)
        {
            if(a[i] == 1 && a[i + 1] == 1)
            {
                a[i] = a[i + 1] = 0;
                ans ++;
            }
        }
        for(int i = 1; i <= n; i ++)
        {
            ans += a[i] * 2;
        }
        puts("YES");
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值