思路:

首先如果数列的最大公约数大于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(即把所有的数都变成偶数)。

奇数 + 奇数 = 偶数,奇数 + 偶数 = 奇数,偶数 + 偶数 = 偶数。把奇偶性不同的相邻的两个数都变成偶数需要2次操作,而把相邻的两个奇数都变成偶数需要1次操作,所以首先优先把相邻的奇数处理掉,再处理奇数和偶数相邻的情况。

实现:

 #include <iostream>
using namespace std; int gcd(int a, int b)
{
return !b ? a : gcd(b, a % b);
} int n, a[]; int main()
{
cin >> n;
int g = ;
for (int i = ; i < n; i++)
{
cin >> a[i];
if (!i) g = a[i];
else g = gcd(g, a[i]);
}
if (g > )
{
puts("YES\n0\n"); return ;
}
int cnt = ;
for (int i = ; i < n; i++)
{
if (!(a[i] & )) continue;
else if (i + < n)
{
if (a[i + ] & ) cnt++;
else cnt += ;
i++;
}
else cnt += ;
}
cout << "YES" << endl << cnt << endl;
return ;
}

标程:

 # include <bits/stdc++.h>
using namespace std;
# define fi cin
# define fo cout
int main(void)
{
int n;
fi>>n;
int g = ,v,cnt = ,ans = ;
while (n --)
{
int v;
fi>>v;
g = __gcd(g,v);
if (v & ) ++cnt;
else ans += (cnt / ) + * (cnt & ),cnt = ;
}
ans += (cnt / ) + * (cnt & );
fo << "YES\n";
if (g == )
fo << ans << '\n';
else
fo << "0\n";
cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
return ;
}

CF798C Mike and gcd problem的更多相关文章

  1. 洛谷 CF798C Mike and gcd problem

    嗯... 题目链接:https://www.luogu.org/problemnew/show/CF798C 这道题首先要会写gcd..也类似一种找规律吧... 问题的操作是在两个数的基础上进行的: ...

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

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  3. CF798 C. Mike and gcd problem

    /* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...

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

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  5. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

  6. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

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

  7. #410div2C. Mike and gcd problem

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

  8. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

  9. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

随机推荐

  1. Oracle SqlPlus导出查询结果

    Oracle SqlPlus导出查询结果 在sqlplus下导出查询的结果保存到本地sql文件中,可以采用如下方式:1.连接数据库: sqlplus xmq/xmqpwd@192.168.1.57:1 ...

  2. 我的arcgis培训照片13

    来自:http://www.cioiot.com/successview-535-1.html

  3. 我的arcgis培训照片8

    来自:http://www.cioiot.com/successview-554-1.html

  4. symfony could not load type 'datetime'

    当用curd生成控制器后,当修改的时候,会有这个提示,解决方法 在orm中通过事务的方式填充时间,然后把生成的form中的文件的时间段去掉 $builder ->add('title') -&g ...

  5. react 项目实战(九)登录与身份认证

    SPA的鉴权方式和传统的web应用不同:由于页面的渲染不再依赖服务端,与服务端的交互都通过接口来完成,而REASTful风格的接口提倡无状态(state less),通常不使用cookie和sessi ...

  6. Qt 插件综合编程-基于插件的OpenStreetMap瓦片查看器client(5) 小结

    经过不断试用与改动,这个查看器终于还是完毕了设计.实现.查看器,顾名思义,没有编辑功能:说的白一点,仅仅是一个以OpenStreetMap为底图的显示装置罢了.和专业GIS相比,这款基于插件的Open ...

  7. Codeforces Round #313 B. Gerald is into Art(简单题)

    B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. java8-接口变化-默认方法-静态方法

    Java 8中允许接口中包含具有具体实现的方法,该方法称为 “默认方法”,默认方法使用 default 关键字修饰. default String getName(){ return "哈哈 ...

  9. 编译android的一些坑

    1 降级gcc g++到4.4 2 参考:http://source.android.com/source/initializing.html来配置环境 3 使用jdk1.6 包括 java java ...

  10. sql compare options

    sql compare project's options Add object existence checks Use DROP and CREATE instead of ALTER Ignor ...