798C - Mike and gcd problem

题意



通过将一组序列中 ai与ai+1 变为 ai-ai+1 与ai+ai+1 的操作将这组序列的gcd变成不为1。



看了题解才会写== ,所以叫做补提嘛QWQ,当
d|a && d|b 时 d|ax+by ,即 d|ai-ai+1 d|ai+ai+1 时,可得 d|2ai, d|2ai+1

从而新序列的gcd一定为2,所以先求出所有数字的gcd(因为我太菜的原因不知道算gcd的复杂度其实不高,log(max(a,b))这样,如果不等于1,直接输出0。

然后再贪心扫两遍就行。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX_NUM   1000003
int num[MAX_NUM];
int dp[MAX_NUM][2];
int exchang[MAX_NUM];
int gcd(int a, int b){
    if( b == 0 )
        return a;
    return gcd(b,a%b);
}
int main(int argc, char const *argv[])
{
    int n;
    scanf("%d",&n);
    for (int i = 1; i <= n; ++i)
        scanf("%d",&num[i]);
    int pre = gcd(num[1],num[2]);
    for (int i = 3; i <= n ; ++i)
        pre =  gcd(pre,num[i]);
    if(pre!=1){
        printf("YES\n");
        printf("0\n");
        return 0;
    }
    int ans = 0;
    for (int i = 2; i <= n; ++i)
    {
        if(num[i-1]&1&&num[i]&1){
            ans++;
            num[i-1] = 0;
            num[i] = 0;
        }
    }
    for (int i = 2; i <= n; ++i)
    {
        if(num[i-1]&1||num[i]&1){
            ans+=2;
            num[i-1] = 0;
            num[i] = 0;
        }
    }
    printf("YES\n%d\n",ans );
    return 0;
}
时间: 2024-12-17 12:34:28

798C - Mike and gcd problem的相关文章

codeforces 798C Mike and gcd problem

C.Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beau

codeforces 798C.Mike and gcd problem 解题报告

题目意思:给出一个n个数序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] 然后想构造一个beautiful的序列 b1,b2, ..., bn,使得最大公约数 gcd(b1,b2,...,bn) > 1.任意ai,ai+1 可以用 ai-ai+1, ai+ai+1 来替换. 问序列 a 构造成 b 的最小操作次数 首先,这个题目是肯定有解的,也就是恒输出yes 试想一下,相邻两个数之间无非就是四种情况: (1)对于同偶情况,不需要做转换,公约数直接为2:

G - Mike and gcd problem

G - Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it be

CF Round410 C. Mike and gcd problem

C. Mike and gcd problem 一奇一偶需要两次操作,两个奇数需要一次操作. 798D - Mike and distribution In the beginning, it's quite easy to notice that the condition " 2·(ap1?+?...?+?apk) is greater than the sum of all elements in A " is equivalent to " ap1?+?...?+?a

【算法系列学习】codeforces C. Mike and gcd problem

C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 8 using namespace std; 9 const

Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)

传送门 Description Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful.

CodeForce-798C Mike and gcd problem(贪心)

Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he c

CF798C Mike and gcd problem

思路: 首先如果数列的最大公约数大于1,直接输出即可. 否则,设对原数列中的ai和ai+1进行一次操作,分别变为ai - ai+1和ai + ai+1.设新数列的最大公约数为d,则由于d|(ai - ai+1)并且d|(ai + ai+1)得到d|(2ai)且d|(2ai+1).则d|gcd(a1, a2, ..., 2ai, 2ai+1, ai+2, ..., an)|2gcd(a1, a2, ..., an) = 2.说明进行一次这样的操作最多可以把最大公约数变为原来的2倍.所以我们的目标就

2.7 编程之美--最大公约数的3种解法[efficient method to solve gcd problem]

[本文链接] http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html [题目] 求两个正整数的最大公约数Greatest Common Divisor (GCD).如果两个正整数都很大,有什么简单的算法吗?例如,给定两个数1 100 100 210 001, 120 200 021,求出其最大公约数. [解法] [1. 辗转相除法] 辗转相除法:f(x,y) = f(y , x % y)(x>y