CF798C Mike and gcd problem
思路:
首先如果数列的最大公约数大于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的更多相关文章
- 洛谷 CF798C Mike and gcd problem
嗯... 题目链接:https://www.luogu.org/problemnew/show/CF798C 这道题首先要会写gcd..也类似一种找规律吧... 问题的操作是在两个数的基础上进行的: ...
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- CF798 C. Mike and gcd problem
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...
- 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 ...
- 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); ...
- 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 ...
- #410div2C. Mike and gcd problem
C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- Ubuntu 16.04安装Markdown编辑器MarkMyWords
继上一篇文章http://www.cnblogs.com/EasonJim/p/7119345.html中使用Sublime Text 3进行Markdown的编辑,总觉得操作上比较繁琐,现在推荐使用 ...
- JDBC的结果集
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/result-sets.html: SQL语句从数据库查询中获取数据,并将数据返回到结果集中.SELEC ...
- MYSQL 时间数据类型
- mybatis bug之resultmap缺少object-relation匹配参数password,造成设置密码不成功
1.mybatis bug之resultmap缺少object-relation匹配参数password,造成设置密码不成功 在resultmap里没有设置user类中password属性和数据库表t ...
- 007 Vlan config
sw0(config)#vlan 2 sw0(config-vlan)#name Sales sw0(config-vlan)#vlan 3 sw0(config-vlan)#name Tech sw ...
- centos7容量扩充
新买的2T 绿盘到货了~~好开心的说~但毕竟是第一次安装,事先还是在网上搜索了很多资料才敢动手,下面就开始啦~ 环境:Centos7.dell服务器.2T容量绿盘 1.硬盘连接好之后,开机先使用fdi ...
- RelativeLayout不能居中的解决的方法
在LinearLayout中有个让元素居中的办法就是.比方在LinearLayout里有个TextView.设置TextView的gravity能够让其居中. 而在Realative里设置这个不起作用 ...
- BNU 25593 Prime Time 记忆化dp
题目链接:点击打开链接 题意: 一个游戏由3个人轮流玩 每局游戏由当中一名玩家选择一个数字作为開始 目的:获得最小的得分 对于当前玩家 O .面对 u 这个数字 则他的操作有: 1. 计分 u +1 ...
- js逻辑执行判断
两个变量或者函数,如果与的关系,a && b,如果a是真则在运行b,如果a是假则不运行b了:如果是或的关系,前者是真则不运行后边的了,否则反过来. 举个例子: <span cla ...
- Django打造大型企业官网(七)
4.13.新闻列表tab栏布局完成 templates/news/index.html <div class="list-outer-group"> <ul cl ...