题目链接

比较棒的一道题,

题意: 给你一个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 (贪心思维+数论)的更多相关文章

  1. Mike and gcd problem CodeForces - 798C

    题目 (智商题 or 糟心的贪心) 题意: 有一个数列a1,a2,...,an,每次操作可以将相邻的两个数x,y变为x-y,x+y,求最少的操作数使得gcd(a1,a2,...,an)>1.gc ...

  2. 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 ...

  3. 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 ...

  4. 【算法系列学习】codeforces C. Mike and gcd problem

    C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...

  5. 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); ...

  6. CF798 C. Mike and gcd problem

    /* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1 ...

  7. #410div2C. Mike and gcd problem

    C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. codeforces 798 C. Mike and gcd problem(贪心+思维+数论)

    题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...

  9. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

随机推荐

  1. ajax调用WebService实现数据库操作

    首先说下测试环境和思路: 前端收集数据转换成json格式传输到后端,处理并存入数据库 1.数据库操作: [WebMethod] public string InsertPoint(string dat ...

  2. java应用系统运行速度慢的解决方法

    场景:我们在部署了TOMCAT应用,刚刚开始启动的一个段时间内.访问系统的速度比较快.但是过了一段时间,应用系统就慢慢的变慢起来了.服务的访问加载时间慢慢变长. 问题解决思路: 1,查看部署应用系统的 ...

  3. Windows 的命令行安装Scoop程序管理工具

    传送门: # 官网 http://scoop.sh/ # github https://github.com/lukesampson/scoop window中快速安装: 必须使用powershell ...

  4. HttpHandler实现网页图片防盗链

    using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...

  5. Hibernate 5 入门指南-基于JPA

    首先创建\META-INF\persistence.xml配置文件并做简单的配置 <persistence xmlns="http://java.sun.com/xml/ns/pers ...

  6. 排序算法之冒泡排序的思想以及Java实现

    1 基本思想 设排序表长为n,从后向前或者从前向后两两比较相邻元素的值,如果两者的相对次序不对(A[i-1] > A[i]),则交换它们,其结果是将最小的元素交换到待排序序列的第一个位置,我们称 ...

  7. Unity Shader 基础(2) Image Effect

    Unity中 Image Effect 是Post Processing的一种方,Unity自身也提供很多Effect效果供使用.Image Effect的使用官方文档做了很多介绍,这里重点Post ...

  8. 设置Firefox自动清除缓存,无需手动清除

    1.在firefox的地址栏上输入about:config回车 2.找到browser.cache.check_doc_frequency选项,双击将3改成1保存即可. 那么这个选项每个值都是什么含义 ...

  9. C#深度学习の委托深度解析

    一.我们在使用C#的过程中,不可避免的用到了委托. 委托的本质是什么呢? 从语法上看,委托是对方法的抽象封装,例如:public void print1(),public void print2(), ...

  10. 详解区块链P2P网络

    根据前一篇文章<从微观到宏观理解区块链>我们已经了解到,微观上,区块链本质就是一种不可篡改且可追踪溯源的哈希链条:宏观上,还具备了另外三个基本特征:分布式存储.P2P 网络和共识机制.分布 ...