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, ...
随机推荐
- web前端(7)—— 了解CSS样式,引入css样式的方式
CSS 在前面大概的介绍了css,从本片博文开始,会详细的介绍它,在最开始介绍web前端时,打开百度首页,当时我提出了一个问题,为什么百度首页的输入框可以放在正中间,就是由于有css的控制,我们可以打 ...
- SQLServer基础之数据页类型:GAM,SGAM,PFS
简介 我们已经知道SQL Server IO最小的单位是页,连续的8个页是一个区.SQL Server需要一种方式来知道其所管辖的数据库中的空间使用情况,这就是GAM页和SGAM页. GAM页 GAM ...
- MySQL数据库有哪些安全相关的参数需要修改?
https://dev.mysql.com/doc/refman/5.7/en/security-options.htmlhttps://dev.mysql.com/doc/refman/5.7/en ...
- Linux核心调度器之周期性调度器scheduler_tick--Linux进程的管理与调度(十八)
我们前面提到linux有两种方法激活调度器:核心调度器和 周期调度器 一种是直接的, 比如进程打算睡眠或出于其他原因放弃CPU 另一种是通过周期性的机制, 以固定的频率运行, 不时的检测是否有必要 因 ...
- UGUI自定义组件之Image根据Text大小自动调整
需求分析 在之前的文章中,介绍到可以使用UGUI自带的ContentSizeFitter组件,进行Button根据Text的长度自适应, UGUI ContentSizeFitter之Button根据 ...
- 更换jupyter notebook风格主题、修改默认工作路径(Ubuntu系统和Win系统)
默认的风格对代码关键词的颜色提醒很不明显,而且白色背景长久使用非常刺眼,所以考虑更换主题. 在更换途中,发现代码输出行,前几个字符被遮挡显示不出来,找了很久才解决,备忘一些要点. 1:替换主题方法 h ...
- 强大的Notepad++,竟然还是自由使用的
这么好用的工具,竟然还是可以自由使用的的,当然就不用去找某些软件的破解版了. 除了本身很好用,还有插件功能,插件许多也是自由使用的,利用插件就可以实现程序员需要的一个手工编辑器了.
- 【Linux基础】VI命令模式下大小写转换
[开始位置] ---- 可以指定开始的位置,默认是光标的当前位置 gu ---- 把选择范围全部小写 gU ---- 把选择范围全部大写 [结束位置] ---- 可以跟着类似的w,6G,gg等定位到错 ...
- Nginx使用教程(三):Nginx配置性能优化之I/O和TCP配置
配置Nginx I/O <br\> Sendfile 当应用程序传输文件时,内核首先缓冲数据,然后将数据发送到应用程序缓冲区. 应用程序反过来将数据发送到目的地. Sendfile方法是一 ...
- 设计模式C++实现——装饰者模式
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/walkerkalr/article/details/28633123 模式定义: 装 ...