codeforces798C - Mike and gcd problem (数论+思维)
原题链接:http://codeforces.com/contest/798/problem/C
题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的。存在这样的操作,使ai,ai+1变为(ai-ai+1), (ai+ai+1)。问最少进行这样的操作使数列是“漂亮”的。
思路:考虑gcd(a1,a2,a3...,an)>1 的情况:
我们对ai,ai+1进行两次操作可以得到2ai,2ai+1,也就是说一对相邻的数字最多操作2次使它们的gcd=2>1。而对于一对奇数来说,操作一次就能使它们成为偶数。
现在就是要把数列中所有的数变为偶数。先对相邻奇数进行操作(每次+1),再对单个奇数进行操作(每次+2)即可。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXN=2e6+;
long long num[MAXN];
long long gcd(long long a, long long b){
return (b==)?a:gcd(b, a%b);
}
int main()
{
int n;
while(cin>>n){
memset(num, , sizeof(num));
for(int i=;i<n;i++){
cin>>num[i];
}
long long g=gcd(num[], num[]);
for(int i=;i<n;i++){
g=gcd(g, num[i]);
if(g==)
break;
}
if(g>){
cout<<"YES"<<endl;
cout<<<<endl;
continue;
}
int res=;
for(int i=;i<n;i++){
if(num[i-]%&&num[i]%){
num[i-]++;
num[i]++;
res++;
}
}
for(int i=;i<n;i++){
if(num[i-]%&&num[i]%==||(num[i]%&&num[i-]%==)) {
num[i-]=;
num[i]=;
res+=;
}
}
cout<<"YES"<<endl;
cout<<res<<endl;
} return ;
}
codeforces798C - Mike and gcd problem (数论+思维)的更多相关文章
- CF798 C. Mike and gcd problem
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- 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 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- Mike and gcd problem CodeForces - 798C (贪心思维+数论)
题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...
- Codeforces 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
随机推荐
- Vagrant 手册之 Provisioning - Shell 配置程序
原文地址 Provisioner 命令:"shell" 示例: node.vm.provision "shell" do |s| s.inline = < ...
- mysqladmin - 管理 MySQL 服务器、获取运行状态
官方文档 mysqladmin 是管理 MySQL 服务器的客户端,可以用来检测服务器的配置和当前状态.创建和删除数据库等. 1. mysqladmin 的调用语法 shell> mysqlad ...
- 【小刘的linux学习笔记 】——01认识操作系统
1.操作系统的地位 计算机系统由硬件和软件两部分组成.通常把未配置软件的计算机称为裸机.直接使用裸机不仅不方便,而且将严重降低工作效率和机器的利用率. 操作系统(OS,Operation System ...
- Expected one result (or null) to be returned by selectOne() 数据库结果集和java实例
mybatis会根据查询的结果集初始化java实例. 如果是复杂类型,我们一般都会在mapper中做好映射. 1.所以如果查询到的是多个结果,那么对应的java类型也必须的集合类型.(result 为 ...
- pipenv虚拟环境
虚拟环境 之前用的 virtualenv +virtualenvwrapper 今天在学习 flask 框架 用到了pipenv pipenv Pipfile 文件是 TOML 格式而不是 ...
- 转 router-view 的理解
主要是构建 SPA (单页应用) 时,方便渲染你指定路由对应的组件.你可以 router-view 当做是一个容器,它渲染的组件是你使用 vue-router 指定的.比如: 视图层: <div ...
- [Bzoj1009][HNOI2008]GT考试(动态规划)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 显而易见的动态规划加矩阵快速幂,不过转移方程不怎么好想,dp[i][j]表示长度为 ...
- 最小公倍数(lcm与gcd)
J - Worker Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard p ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)
题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...
- bzoj2396 神奇的矩阵(随机化)
Time Limit: 5 Sec Memory Limit: 512 MB 给出三个行数和列数均为N的矩阵A.B.C,判断A*B=C是否成立. 题目可能包含若干组数据. 对于每组数据,第一行 ...