zoukankan      html  css  js  c++  java
  • Codeforces Round #410 (Div. 2)C题

    C. Mike and gcd problem
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.

     is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

    Input

    The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.

    The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

    Output

    Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.

    If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

    Examples
    input
    2
    1 1
    output
    YES
    1
    input
    3
    6 2 4
    output
    YES
    0
    input
    2
    1 3
    output
    YES
    1
    Note

    In the first example you can simply make one move to obtain sequence [0, 2] with .

    In the second example the gcd of the sequence is already greater than 1.

    题意:给一串数,求gcd,如果gcd==1,那么可以通过改变a[i],a[i+1],变成a[i]-a[i+1],a[i]+a[i+1],求最小改变次数

    题解: 比赛时没做出来,一直以为要用gcd模拟!(真是越来越蠢,忘了数论题推公式很重要了!)

    证明:设d是改变后的gcd,d|a[i]-a[i+1],d|a[i]+a[i+1],得d|2*a[i],d|2*a[i+1],

    那么d|gcd(a[0],...2*a[i],2*a[i+1],...,a[n-1]),d|2*gcd(a[0],...a[i],a[i+1],...,a[n-1])

    而gcd(a[0],...a[i],a[i+1],...,a[n-1])==1,则d==2;

    最后通过把每个数%2计算总和

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const int N=100000+5,maxn=100000+5,inf=0x3f3f3f3f;
    
    ll n,a[N],ans=0;
    
    ll gcd(ll a,ll b)
    {
        return b? gcd(b,a%b):a;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n;
        ll ans=0,x;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            if(i==0)x=a[i];
            else x=gcd(x,a[i]);
            a[i]%=2;
        }
        if(x>1)
        {
            cout<<"YES"<<endl<<0<<endl;
            return 0;
        }
        for(int i=0;i<n;i++)
        {
            if(a[i]==1)
            {
                if(i+1<n)
                {
                    if(a[i+1]==1)ans++,a[i+1]=0;
                    else if(a[i+1]==0)ans+=2;
                    a[i]=0;
                }
                else ans+=2;
            }
        }
        cout<<"YES"<<endl<<ans<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    php socket 客户端代码
    linux crontab定时执行
    加载 pcntl 多进程
    Xdebug 配置
    Zend Debugger 配置
    windows SVN搭建
    深度学习笔记:优化方法总结(BGD,SGD,Momentum,AdaGrad,RMSProp,Adam)
    操作系统-分段机制
    C++中的new、operator new与placement new
    线程安全的概念
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6747672.html
Copyright © 2011-2022 走看看