Mike and gcd problem CodeForces - 798C (贪心思维+数论)
比较棒的一道题,
题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1
如果可以输出YES和最小的次数,否则输出NO
首先我们来看一下这个操作,
如果对 a b 老两个数进行操作
第一次为 a-b a+b
第二次为 -2b 2a
由此可见,任何两个数最多进行两次操作,可以让他们都能被2整除。
所以也就没有NO的情况。
那么我们只需要预处理一下gcd,如果>1了,直接输出0次。
gcd=1的话,那么就需要我们去处理这个字符串了。又上边的推导可见,我们以gcd=2为目标去实现是最优解。
我们进入循环,找到一个a[i] 是odd的话,我们就和a[i+1] 进行处理,然后记录操作的次数,以此处理整个数组。
最终得出答案。
其他的一些题解讲到要先处理两个一起的odd再处理和even一起的odd,
我实在不懂为什么要这么分优先级,直接一个for遇到odd就去处理就可以是最优解,无语多虑。
具体细节请看代码:
my code :
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
ll a[maxn];
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
int main()
{
gg(n);
ll x=0ll;
repd(i,,n)
{
scanf("%lld",&a[i]);
x=gcd(x,a[i]);
}
if(x>)
{
printf("YES\n");
printf("0\n");
}else
{
ll ans=0ll;
repd(i,,n)
{
while(a[i]%!=)
{
ans++;
if(i!=n)
{
ll y1=a[i];
ll y2=a[i+];
a[i]=y1-y2;
a[i+]=y1+y2;
}else
{
ll y1=a[i-];
ll y2=a[i];
a[i-]=y1-y2;
a[i]=y1+y2;
}
}
}
printf("YES\n");
printf("%lld\n",ans); } return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Mike and gcd problem CodeForces - 798C (贪心思维+数论)的更多相关文章
- Mike and gcd problem CodeForces - 798C
题目 (智商题 or 糟心的贪心) 题意: 有一个数列a1,a2,...,an,每次操作可以将相邻的两个数x,y变为x-y,x+y,求最少的操作数使得gcd(a1,a2,...,an)>1.gc ...
- 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 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 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 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- Codeforces 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
随机推荐
- CentOS6.5内 Oracle 11GR2静默安装
一.修改配置文件 1.1.修改/etc/security/limits.conf文件,修改用户的SHELL的限制. 输入命令:vi /etc/security/limits.conf,将下列内容加入该 ...
- LeetCode算法题-Construct Quad Tree(Java实现)
这是悦乐书的第224次更新,第237篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第91题(顺位题号是427).我们想使用四叉树来存储N×N布尔网格.网格中的每个单元格只 ...
- February 22nd, 2018 Week 8th Thursday
Confine yourself to the present. 着眼当下. The morning wind spreads its fresh smell, we should get up an ...
- 删除window10没用的服务
最近学习了下resin,出了个问题,它默认端口是8080,跟Tomcat冲突了,我在使用的时候遇到了个奇怪的事情,resin4.0一直占用着我的8080端口,哪怕我用dos命令把它强制停止,不出五秒钟 ...
- vue-cli3安装创建项目以及目录结构
安装脚手架cli3.0 全局安装 npm install -g @vue/cli 或 yarn global add @vue/cli 查看版本/是否安装成功 vue -V 如果你仍然需要使用旧版本的 ...
- DP 魔族密码 LIS
题目描述 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之子:我呕……(杀死人的眼神)快说题目!否则……-_-### 花花:……咦好冷我们现在 ...
- dp 动态规划 蘑菇
蘑菇真的贵,友情价更高 Description 由于提莫为巡逻准备的蘑菇太多了,多余的蘑菇路上种不下,于是他精心挑选了一些蘑菇拜访他的好朋友小炮 提莫的蘑菇一共有n个,对于编号为i的蘑菇魔力值是a ...
- 如何设计一个"好的"测试用例?
什么才算是“好的”测试用例? 好的测试用例一定是一个完备的集合,它能够覆盖所有等价类以及各种边界值,而跟能否发现缺陷无关. "好的"测试用例必须具备哪些特征? 一个“好的”测试用例 ...
- 在 Linux 中自动配置 IPv6 地址
在 Linux 中自动配置 IPv6 地址 在本文中,我们将学习如何为 ULA 自动配置 IP 地址. 何时使用唯一本地地址 唯一本地地址unique local addresses(ULA)使用 f ...
- [matlab] 15.罚函数降维
求非线性规划 min f(x)= x(1)^2 + x(2)^2 + 8 s.t. { x(1)^2-x(2)>=0 , -x(1) - x(2)^2 +2 = 0, x(1)>=0 ,x ...