【codeforces 798C】Mike and gcd problem
【题目链接】:http://codeforces.com/contest/798/problem/C
【题意】
给你n个数字;
要求你进行若干次操作;
每次操作对第i和第i+1个位置的数字进行;
将
a[i]变为a[i]-a[i+1],a[i+1]变为a[i]+a[i+1];
然后问你能不能通过以上变换使得最后所有元素的gcd>1
【题解】
答案总是存在的;
因为把这个操作进行两次可以得到-2*a[i+1],2*a[i]
也就是如果对每相邻的元素进行操作,最后都能使得这两个元素都变成偶数;
最后gcd最少都为2了;
则:
如果一开始所有元素的gcd>1则直接输出0;
等于1;
假设改变一次后的gcd为d
则d|a[1..i-1]且d|a[i+2..n]
且d|a[i]-a[i+1]且d|a[i]+a[i+1]
因为由d|x和d|y可以得到
d|x+y以及d|x-y
所以
d|2*a[i]且d|2*a[i+1]
这样就大概猜测要使得每个元素都变成偶数吧.
每个元素都贪心地让他变成偶数就好了;
(有了“这个操作进行两次可以得到-2*a[i+1],2*a[i]”这个结论,我们可以放心地进行贪心,当然不一定是都要进行两次操作,比如a[i]和a[i+1]都是奇数,则只要进行一次操作,因为一次操作后两个数的奇偶性都会因为加、减了一个奇数而发生改变);
只要第i个元素没有变成偶数就一直进行这个操作;
然后第n个元素如果最后在前n-1个元素进行操作之后没有变成偶数;
则必然可以对n-1,n操作两次使得a[n-1]保留仍旧是偶数,a[n]也变成了偶数;
因为
a[n-1]是偶数了,a[n]是奇数
第一次操作后
a[n-1]变成奇数,a[n]还是奇数;
再进行一次操作后
a[n-1]变成偶数,a[n]也变成偶数;
具体看代码
【Number Of WA】
1
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const int INF = 21e8;
int n;
LL a[N],now;
LL gcd(LL a,LL b)
{
if (b==0)
return a;
else
return gcd(b,a%b);
}
LL tgcd(LL a,LL b)
{
return gcd(abs(a),abs(b));
}
void change(int x,int y)
{
LL ta = a[x]-a[y],tb = a[x]+a[y];
a[x] = ta,a[y] = tb;
}
int main()
{
//cout << tgcd(12,3)<<endl;
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf就别用了!
cin >> n;
rep1(i,1,n)
cin >> a[i];
now = tgcd(a[1],a[2]);
rep1(i,3,n)
now = tgcd(now,a[i]);
cout <<"YES"<<endl;
if (now>1)
{
cout <<0<<endl;
return 0;
}
int ans = 0;
for (int i = 1;i <= n-1;i++)
{
while ((a[i]%2)!=0)
{
change(i,i+1);
ans++;
}
}
while ((a[n]%2)!=0)
{
change(n-1,n);
ans++;
}
cout << ans << endl;
return 0;
}
【codeforces 798C】Mike and gcd problem的更多相关文章
- 【codeforces 798A】Mike and palindrome
[题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...
- 【codeforces 798B】Mike and strings
[题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
- 【codeforces 742B】Arpa’s obvious problem and Mehrdad’s terrible solution
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 798D】Mike and distribution
[题目链接]:http://codeforces.com/contest/798/problem/D [题意] 让你选一个下标集合 p1,p2,p3..pk 使得2*(a[p1]+a[p2]+..+a ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- 【算法系列学习】codeforces C. Mike and gcd problem
C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html #include<iostream> #includ ...
- 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#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); ...
随机推荐
- 在android系统调试中使用tinyalsa命令【转】
本文转载自:http://blog.csdn.net/tangdexi112/article/details/17579021 我们在进行音频调试的时候,需要使用tinymix.tinyplay.ti ...
- mac os lscpu 【转】
CPU Information on Linux and OS X This is small blog post detailing how to obtain information on you ...
- B1232 [Usaco2008Nov]安慰奶牛cheer 最小生成树
%%%小詹太巨啦!!!我就想直接最小生成树之后建树跑dfs,然后写跪了...然后看小詹博客之后恍然大悟,原来直接把边权改为w * 2 + 两边点权值就行了. 但是还是不对,为什么呢?原来我们起点走了三 ...
- 5-8 第五天 微信 JS-SDK
SDK的难点不多,但是容易出错的地方特别多.几乎任何一个环节都会犯错误. 难道没有域名就不让你测了吗?别担心,有公众测试号, 域名这个代理呢是通过QQ浏览器的服务器调试,来绑定这个端口的,把这个服务开 ...
- centos7用rpm安装mysql5.7【初始用yum安装发现下载非常慢,就考虑本地用迅雷下载rpm方式安装】
1.下载 4个rpm包 mysql-community-client-5.7.26-1.el7.x86_64.rpmmysql-community-common-5.7.26-1.el7.x86_64 ...
- 爬虫框架Scrapy与Web框架Django结合
在做两者结合之前,需要先准备一个可以独立运行的Scrapy框架和一个可以独立运行的Django框架! 当准备好这两个框架之后,就可以做两者的结合了. 一. 把scrapy框架,移动到Django框架的 ...
- SQL学习--Select(一)TOP、派生表、连接、谓词
TOP关键字 WITH TIES t.title,sum(s.qty)as totalSales from sales s left join titles t on s.title_id=t.tit ...
- Android自定义开机和关机动画
Android自定义开机和关机动画 Android在开机的过程中,会经历三张图片,关于静态图的修改在我的这篇文章中有介绍到: Android开机图片替换 现在要介绍的是怎么用动画替换静态图片.开/关机 ...
- OPPO R9sPlus MIFlash线刷TWRP Recovery ROOT详细教程
教程转载来自 残芯此生不换 OPPO R9sPlus 目前最简单的刷Recovery root 方法,强烈推荐 新机想要刷第三方卡刷包的最简单过程是: 手机关机-->下载M ...
- [CefSharp] 如何在JavaScript中调用C#代码
本例在WinForms下实现,具体流程与WPF一致. 本例仅供调用示例,不代表正常业务书写流程. 1. 创建WinForms项目,并将项目属性设置为x86平台 此处预先设置,避免引用时报错,再花更多的 ...