Mike and gcd problem CodeForces - 798C
(智商题 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的更多相关文章
- Mike and gcd problem CodeForces - 798C (贪心思维+数论)
题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...
- 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 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- iframe 框架中 父子界面的JS调用
子界面调用父界面 window.parent.hello(); 父界面调用子界面 window.frmaes[i].hello();
- C标准库中atoi的一种可能的实现
为避免与标准库中的atoi产生歧义, 我将自己编写的函数命名为strToInt, 以下是示例代码 #include <stdio.h> int strToInt(const char *s ...
- poj 1179 $Polygon$(断环成链)
Polygon \(solution:\) upd:还是多讲一下,这道题基本上可以说是一道思维题.一道结论题.一道考验你动态规划基本功是否扎实的题目.因为这道题的数据范围很小,思考一下总能想到断环成链 ...
- UVA11270 Tiling Dominoes —— 插头DP
题目链接:https://vjudge.net/problem/UVA-11270 题意: 用2*1的骨牌填满n*m大小的棋盘,问有多少种放置方式. 题解: 骨牌类的插头DP. 1.由于只需要记录轮廓 ...
- easyui 日期范围前后台的设置以及实现
1.页面部分(引入相应的js) <td class="w40 tl pl10">从日期:</td> <td> <input class=& ...
- Oracle:通过dbv查看数据文件是否有坏块
我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...
- hdu 4544 湫湫系列故事——消灭兔子(优先队列)
题意:n只兔子(有血量),m只箭(有伤害.花费),每只兔子只能被射一次,求射死所有兔子的最少花费. 思路:贪心,2重循环,兔子从血量高到低,箭从伤害高到低,用能射死兔子的箭中花费最小的箭射. #inc ...
- UIButton常见属性和方法
一.创建,两种方法: 1. 常规的 initWithFrame UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 8 ...
- Ubuntu bitnami gitlab 安装
/************************************************************************************** * Ubuntu bit ...
- CodeForces-668D:Remainders Game (中国剩余定理||理解)
Today Pari and Arya are playing a game called Remainders. Pari chooses two positive integer x and k, ...