题目

(智商题 or 糟心的贪心)

题意:

有一个数列a1,a2,...,an,每次操作可以将相邻的两个数x,y变为x-y,x+y,求最少的操作数使得gcd(a1,a2,...,an)>1。gcd(a1,...,an)表示最大的非负整数使得所有ai都能被gcd(a1,...,an)整除。

分析

首先,如果原来gcd就不是1那么答案就是0。
如果gcd是1:
相邻两数x和y的变化方法为:x,y=>x-y,x+y
设新的gcd值为d,那么x-y和x+y能被d整除,因此2x和2y能被d整除,
因此d最大是x与y的gcd的2倍
因此只需要考虑如何使所有数变为偶数(这样次数一定是最小的)
x奇y偶
=>x奇y奇
x偶y奇
=>x奇y奇
x偶y偶
=>x偶y偶
x奇y奇
=>x偶y偶
将偶数标记为0,奇数标记为1,
那么就变成了xor运算
整题就变成了给定一个由0/1组成的数列,每次可以将相邻两个数都变为它们异或的结果,
最后要使所有数变成0
这样就很容易想出贪心做法。如果有k个连续的1:
如果k是偶数,那么需要k/2次操作变为全0.
如果k是奇数,那么需要[k/2]次操作将k-1个变为全0,然后剩下一个与旁边的0
(由于前面[k/2]次操作产生了0,那一个旁边一定有0)进行2次操作变为全0.

 #include<cstdio>
int d,n,ans;
int a[],b[],num_b;
int gcd(int a,int b)
{
int t;
while(b!=)
{
t=a;
a=b;
b=t%b;
}
return a;
}
int main()
{
int i;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
d=gcd(d,a[i]);
a[i]%=;
}
printf("YES\n");
if(d!=)
{
printf("0\n");
return ;
}
if(a[]==)
b[++num_b]++;
for(i=;i<=n;i++)
{
if(a[i]==&&a[i-]==)
b[++num_b]++;
else if(a[i]==)
b[num_b]++;
}
for(i=;i<=num_b;i++)
{
if(b[i]%==)
ans+=b[i]/;
else
ans+=b[i]/+;
}
printf("%d\n",ans);
return ;
}

Mike and gcd problem CodeForces - 798C的更多相关文章

  1. Mike and gcd problem CodeForces - 798C (贪心思维+数论)

    题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...

  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 798C - Mike and gcd problem(贪心+数论)

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

  9. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

随机推荐

  1. CentOS笔记-系统概述

    Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 当计算机打开电源后,首先是BIOS开机自检,按照BIOS中设置的启动设备(通常是硬盘)来启动. 操作系统接管 ...

  2. React通用后台管理系统

    react-admin 部分采用了antd,相关功能较全,添加了较多的组件模块.star 664 GitHub:https://github.com/yezihaohao/react-admin 在线 ...

  3. eclipse输入提示 设置

  4. POJ3258 River Hopscotch —— 二分

    题目链接:http://poj.org/problem?id=3258 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. Android Studio运行出现项目 错误:非法字符:“\ufeff”

    导入项目时,遇到编码问题. 主要原因是: eclipse可以自动把UTF-8+BOM文件转为普通的UTF-8文件,但Android Studio需要重新转换一下编码 解决方法: 将编码格式UTF-8+ ...

  6. HDU5890:Eighty seven(Bitset优化背包)

    Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach ch ...

  7. Burpsuite实验一

    一.实验准备 win7系统 burpsuite 二.实验目的 进行重放攻击,观察结果 三.实验内容 本次实验并没有采取在虚拟机下进行,而是直接在win7系统中进行.首先配置一下环境,选择你常用的浏览器 ...

  8. 「LuoguP3369」 【模板】普通平衡树 (用vector乱搞平衡树

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入 x 数 删除 x 数(若有多个相同的数,应只删除一个) 查询 x 数的排名(排名定义为比当前 ...

  9. 数据类型总结 (C++)

    数据类型总结 (C++) 作用 C++ 大小 取值范围 后缀 字符 char 1 -128~127   字符(无符号) unsigned char 1 0~255   宽字符 wchar_t 2   ...

  10. ibatis 中 $与#的区别

    ibatis 中 $与#的区别 使用#: select * from table where id = #id# 如果字段为整型:#id#表示成id select * from table where ...