思路:

首先如果数列的最大公约数大于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. 文件I/O和标准I/O

    转载:https://blog.csdn.net/kyang_823/article/details/79496561 一.文件I/O和标准I/O文件I/O:文件I/O也称为不带缓冲的I/O(unbu ...

  2. Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后没有Windows 10的启动菜单解决方法

    背景: 硬盘分区方式:MBR 硬盘容量256,Windows 100,Ubuntu 156,其中主分区安装的是Windows,Ubuntu安装在逻辑分区上,文件系统为Ext4,整个Ubuntu就挂载在 ...

  3. MongoDB小结16 - find【查询条件$in】

    $in可以查询一个键的多个值 举例,每个人有爱好,假定为一个,数据太多,咱们用第二个参数来过滤一下 db.user.find({},{"_id":0}) { "hobby ...

  4. Eclipse 远程tomcat调试程序

    Eclipse 远程tomcat调试程序 很多时候我们把代码部署到云服务器上,需要调试的时候可以选择远程调试,既节省时间,效率又高.下面详细介绍如何进行远程调试. 1.1. 创建startup-deb ...

  5. CentOS firewall添加开放端口

    添加 firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效) 重新载入 firewal ...

  6. android仿qq空间、微信朋友圈图片展示

    废话不多说,先上效果图 由于近期须要做朋友圈功能,所以在此记录一下,事实上非常多人不明确的一点应该是在图片的排列上面吧,不规则的排列,事实上非常easy的.就是一个GridView.然而你xml光光写 ...

  7. 用Java开发50个棋类游戏

    眼下已经开发完了两个 1A2B 24点 打算开发以下的.直接在QQ上玩. QQ机器人已经有了.我们直接写业务即可.有兴趣的參与.机器人婷婷体验群 Java技术交流 207224939 四棋 小枪大炮 ...

  8. react新特性 react hooks

    本文介绍的是react新特性react hooks,本文面向的是有一定react开发经验的小伙伴,如果你对react还不是很熟悉的话我建议你先学习react并多多联系. 首先我们都知道react有3种 ...

  9. 在Unity中接入Xbox360手柄

    提要 本文主要介绍了怎样在Unity中接入Xbox360的手柄. 当我们调Input.GetAxis("Horizontal"),我们调了什么 Unity中全部关于输入的设置都在E ...

  10. solr入门之solr的拼写检查功能的应用级别尝试

    今天主要是收集了些拼写检查方面的资料和 尝试使用一下拼写检查的功能--=遇到了不少问题 拼写检查的四种配置眼下我仅仅算是成功了半个吧 --------------------------------- ...