codeforces#410C Mike and gcd problem
题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1);问需要执行几次这个操作才能使得gcd(a1,a2,...an)>1.
分析:
1.首先,答案总是YES。
2,假设gcd(a1,a2,...an)=1,在一次对ai和ai+1的操作后新的gcd为d,则d满足:d|ai - ai + 1 and d|ai + ai + 1
d|2ai and d|2ai + 1.。同样,因为d是新序列的gcd,所以d也满足: d|aj, j ≠ i, i + 1.
3.从而我们得到:
。所以gcd(a1,a2,...an)=1,则在一次操作后,序列的gcd最多会变得两倍大.(即会变成2.)
4.这意味着如果我们要是序列的gcd大于1的话只用使所有的数为偶数即可。
5.因为两个奇数只通过一次操作即都可以变成偶数,而一奇一偶要通过两次才可以都变成偶数。
6.可以把题目转化为:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,求将所有数变成偶数需要的操作次数
7.又可转化为:求序列中有几段连续为奇数,如果使奇数段中含的数为偶数个k,则需要k/2次,如果为奇数个,则需要(k/2)+2次;
代码:
/*
Problem: C. Mike and gcd problem
Time: 2017/5/5/19:21
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#define ll long long
#define inf 100000000
using namespace std; ll a[];
ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%I64d",a+i);
ll d=a[];
for(int i=;i<n;i++)
d=gcd(d,a[i]);
int ans=;
if(d<=)
{
int odd=;
for(int i=;i<n;i++)
{
if(a[i]%!=) odd++;
else if(odd!=)
{
ans+=(odd%==?(odd/):(odd/+));
odd=;
}
}
//cout<<ans<<endl;
if(odd) ans+=(odd%==?(odd/):(odd/+));
}
printf("YES\n");
printf("%d\n",ans);
return ;
}
补充:(关于DIVISIBILITY AND GREATEST COMMON DIVISORS)
几个定理:
1.. Let a, b ∈ Z with a|b. Then a|bc for any c ∈ Z.
2. If a|b and b|c then a|c.
3.If a|b and a|c then a|(br + cs) for every r and s in Z. In particular, if a|b and a|c then a|(b + c) and a|(b − c).
4.For nonzero a and b in Z, there are x and y in Z such that (3.2) (a, b) = ax + by.
codeforces#410C Mike and gcd problem的更多相关文章
- 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 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(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
- codeforces 798c Mike And Gcd Problem
题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思 ...
- codeforces 798C.Mike and gcd problem 解题报告
题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...
- 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 ...
- 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 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
随机推荐
- 【Maven】---Nexus私服配置Setting和Pom
maven---nexus私服配置setting和pom 上一遍博客已经在linux服务器上,搭建好nexus私服了,博客地址:Linux搭建Nexus3.X私服 现在就需要配置setting.xml ...
- java多线程(7)---Condition
Condition 一.Condition概述 在线程的同步时可以使一个线程阻塞而等待一个信号,同时放弃锁使其他线程可以能竞争到锁. 在synchronized中我们可以使用Object的wait() ...
- FTP解决连接慢问题
今天发现程序报登录FTP超时,于是便手动登录发现真的慢,于是网上搜便获取大招亲测有效,于是怕忘的我马上记录下来,zzzzzzz!! 如下解决 vim /etc/vsftpd/vsftpd.conf 在 ...
- 浅谈Web开发中的定时任务
曾经做过Windows server下的定时任务的业务,最近又做了一些Linux下使用Crontab做的定时任务的业务,觉得有必要进行一次小结,于是有了如下这篇文章. Windows Server下 ...
- 深入理解Spring IOC工作原理
为什么会出现spring,spring出现解决了什么问题? 1.分析普通多层架构存在的问题 JSP->Servlet->Service->Dao 层与层之间的依赖很强,属于耦合而且是 ...
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- github访问很慢解决方案
首先要解决的就是这个访问速度的问题: 获取Github相关网站的ip 访问https://www.ipaddress.com,拉下来,找到页面中下方的“IP Address Tools – Quick ...
- Java开发知识之XML文档使用,解析
目录 XML文件详解 一丶XML简介 1.文档结构 2.XML中的元素(Element)或者叫做标签(Tab).属性 文本内容. 节点(Node) 3.XML语法规则 二丶XML文档解析 三丶使用XP ...
- Git合并不同url的项目
本文由云+社区发表 作者:工程师小熊 摘要:为了让项目能实现Git+Gerrit+Jenkin的持续集成,我们把项目从Git上迁移到了Gerrit上,发现有的同事在老Git提交代码,因为Gerrit做 ...
- Windows 花屏问题
已经有2台电脑 Windows 10 系统出现花屏现象,表现为比较炫的界面出现花屏.文字显示不全.移位.闪烁等,如果点击“设置”.“开始”,Chrome浏览器等:比较平素的界面显示正常,比如资源管理器 ...