codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C
题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 并且把 ai - a(i + 1), ai + a(i + 1)
放入原来的位置。问是否能够在几步操作后使得串的gcd大于1然后要求最小的操作数。
题解:偶数=偶数*偶数 or 奇数*偶数,奇数=奇数*奇数。
如果整个字符串全是偶数的话肯定gcd是大于1的。介于题目要求的操作,奇数-(or)+奇数=偶数
奇数-(or)+偶数=奇数,但是操作两次就是偶数。所以只要把原来串中的奇数改成偶数就行了。
因为影响gcd结果的就是奇数。然后贪心一下就行了。先处理两个奇数连在一起的然后再考虑一奇一
偶的。
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int M = 1e5 + 10;
- int a[M];
- int gcd(int x , int y) {
- return x % y ? gcd(y , x % y) : y;
- }
- int main() {
- int n;
- cin >> n;
- for(int i = 0 ; i < n ; i++) {
- cin >> a[i];
- }
- int num = 0;
- for(int i = 0 ; i < n ; i++) {
- num = gcd(num , a[i]);
- }
- if(num > 1) {
- cout << "YES" << endl;
- cout << 0 << endl;
- }
- else {
- int ans = 0;
- for(int i = 1 ; i < n ; i++) {
- if(a[i] % 2 != 0 && a[i - 1] % 2 != 0) {
- ans++;
- a[i] = 2;
- a[i - 1] = 2;
- }
- }
- for(int i = 0 ; i < n ; i++) {
- if(i == n - 1) {
- if(a[i] % 2 != 0) {
- ans += 2;
- a[i] = 2;
- }
- }
- else {
- if(a[i] % 2 != 0) {
- ans += 2;
- a[i] = 2;
- }
- }
- }
- cout << "YES" << endl;
- cout << ans << endl;
- }
- return 0;
- }
codeforces 798 C. Mike and gcd problem(贪心+思维+数论)的更多相关文章
- codeforces 798 D. Mike and distribution(贪心+思维)
题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...
- 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个位置的数字进行; 将 ...
- 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 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 ...
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- 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); ...
- CF798 C. Mike and gcd problem
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...
- #410div2C. Mike and gcd problem
C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- 100天搞定机器学习|Day13-14 SVM的实现
昨天我们学习了支持向量机基本概念,重申数学推导原理的重要性并向大家介绍了一篇非常不错的文章.今天,我们使用Scikit-Learn中的SVC分类器实现SVM.我们将在day16使用kernel-tri ...
- HDP Hive性能调优
(官方文档翻译整理及总结) 一.优化数据仓库 ① Hive LLAP 是一项接近实时结果查询的技术,可用于BI工具以及网络看板的应用,能够将数据仓库的查询时间缩短到15秒之内,这样的查询称之为Int ...
- Docker:跨主机通信
修改主机docker默认的虚拟网段,然后在各自主机上分别把对方的docker网段加入到路由表中,配合iptables即可实现docker容器夸主机通信.配置方法如下: 设有三台虚拟机 v1: 10.1 ...
- Spring MVC浅入浅出——不吹牛逼不装逼
Spring MVC浅入浅出——不吹牛逼不装逼 前言 上文书说了Spring相关的知识,对Spring来了个浅入浅出,大家应该了解到,Spring在三层架构中主做Service层,那还有Web层,也就 ...
- Mysql索引进阶入门
1. 索引操作 MySQL 索引 菜鸟 2. 索引类型 PRIMARY 唯一且不能为空:一张表只能有一个主键索引 INDEX 普通索引 UNIQUE 唯一性索引 FULLTEXT 全文索引:用于搜索很 ...
- studio无限轮播
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- Nunit与Xunit介绍
Nunit安装 首先说下,nunit2.X与3.X版本需要安装不同的vs扩展. nunit2.x安装 安装如上3个,辅助创建nunit测试项目与在vs中运行单元测试用例 . 1.Nunit2 Test ...
- ABP 配置全局数据过滤器
ABP官方数据过滤的地址:https://aspnetboilerplate.com/Pages/Documents/Data-Filters 中文可以看这个:https://aspnetboiler ...
- (十五)c#Winform自定义控件-键盘(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 阿里、网易和腾讯面试题 C/C++
一.线程.锁 1.Posix Thread互斥锁 线程锁创建 a.静态创建 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; b.动态创建 pthr ...