codeforces 798 C. Mike and gcd problem in number theory, the sequence of gcd> number of changes 1

Topic link: https: //codeforces.com/contest/798/problem/C
subject to the effect: give you the number n, a1, a2, ... an. To that gcd (a1, a2, ... an )> 1, so that the operation may be performed once ai, ai + 1 becomes ai - a [i + 1] , ai + a [i + 1]. Determined such that gcd (a1, a2, ... an )> operation a minimum number required.

Ideas: First, we must know if we can achieve gcd (a1, a2, ... an)> 1, then certainly a1 ~ an even number (0 is an even number), so our aim is to use the least number of operations becomes the number of all even. If the two numbers are odd, the operation that requires time (plus or minus odd an even number), if it is the even and odd, and that requires two operations (addition and subtraction to obtain odd odd and even, then the odd and even-odd obtained subtraction) , so there can not be greater than 1 gcd

#include <bits/stdc++.h>
#define LL long long
using namespace std;
 
int a[100005];
int main()
{
    int n, gcd;
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%d", &a[i]);
        if(i==1){
            gcd=a[i];
        }
        else{
            gcd=__gcd(gcd, a[i]);
        }
    }
    printf("YES\n");
    if(gcd>1){
        printf("0\n");
        return 0;
    }
    int ans=0;
    a[n+1]=2;
    for(int i=1; i<=n; i++){
 
        if(a[i]%2==1&&a[i+1]%2==1){
            ans++;
            i++;
        }
        else if(a[i]%2==1&&a[i+1]%2==0){
            ans+=2;
        }
    }
    printf("%d\n", ans);
 
    return 0;
}
Published 374 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_21433411/article/details/103215865
gcd