即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Codeforces 798C gcd思路题

编程语言 winycg 18℃ 0评论




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.

Example
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




题意:n个数,n<=1e5,操作:把ai,ai+1 替换成 a[i]-a[i+1],a[i]+a[i+1],问gcd(a1,a2..an)>1的最少操作次数 

本题最大的难度在于和奇偶性相结合。两个奇数相加减会变成偶数,一个奇数和偶数相加减还是奇数。

若需要执行操作,那么必定会全部变为偶数才行。(就算2个奇数3,9,一个偶数2,想让奇加偶变为两个奇数从而gcd(a1,a2,a3)>1是行不通的,因为3+2后必定不会再与9不互质)




2个相邻奇数变成2个偶数需要1步,相邻的1个奇数和1个偶数变成2个偶数需要2步,所以先变2个相邻奇数的,再变一个奇数一个偶数的。




#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int a[100010];
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int n;
    while(cin>>n)
    {
        scanf("%d",&a[1]);
        int x=a[1];
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&a[i]);
            x=gcd(x,a[i]);
        }
        if(x>1)
        {
            cout<<"YES"<






转载请注明:CodingBlog » Codeforces 798C gcd思路题

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情