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个位置的数字进行; 将 ...
随机推荐
- 激活IDEA 2019.1
First step: 先下载jar包JetbrainsCrack.jar,把它放到你认为合适的文件夹内, 我放在了安装目录C:\Program Files\JetBrains\IntelliJ ID ...
- mssql存储过程异常处理
MSSQL2000和MSSQL2005以上版本的异常处理语法是不相同的. SQL Server 2005以上版本支持结构化异常处理,而MSSQL2000是不支持的. 1)先看MSSQL 2000的异常 ...
- 使用NPOI将DataTable生成Excel
听闻npoi 2.0版本支持excel2007格式了,表示期待其表现.不过目前还是使用1.2.5稳重点. 生活中有太多的列表都需要一个导出功能,当然这里的生活指的的程序员的生活.DataTable是从 ...
- MAVEN项目模块化
maven的最大的特点之中的一个就是能够把项目模块化. 前面的一篇文章MAVEN创建并打包web项目已经创建了一个简单的webapp,注意这个webapp的打包方式是war. 假设如今又要划分出来一个 ...
- react 执行 yarn build 页面无法显示
资源文件路径问题 如果你使用create-react-app创建项目,执行命令 yarn build 后,直接以静态方式打开build文件夹内的index.html,会看到页面显示出现问题,打开con ...
- 86. LotusScript中的数组函数
R6对LotusScript有一些改进和增强,自那之后.Notes对象的接口时有补充和更新,但语言本身没有变化.那些改进就包括添加诸如ArrayGetIndex.ArrayUnique的实用函数. 但 ...
- 《软件project》——编码
编码的目的是使用选定的程序设计语言,把模块的过程描写叙述翻译为用该语言书写的源程序. 源程序应该正确可靠.简明清晰,并且具有较高的效率.在编程的步骤中,要把软件具体设计的表达式翻译成为编程语言的 ...
- C/C++综合測试题(四)
又刷了一套题 这些题都是百度.阿里巴巴.腾讯.网易.新浪等公司的面试原题.有一定的难度.只是确实相当有水平.能够通过做题来查漏补缺. 11.以下代码的输出是什么? class A { public: ...
- kvm虚拟化网络管理
Linux Bridge 网桥管理 VM2 的虚拟网卡 vnet1 也连接到了 br0 上. 现在 VM1 和 VM2 之间可以通信,同时 VM1 和 VM2 也都可以与外网通信 # Vlan LAN ...
- SQL语句小结
1.创建数据库 create database 数据库名 2.删除数据库 drop database 数据库名 3.创建表 1>.create table 表名 (col1 type1 [no ...