Codeforces Round #410 (Div. 2) C. Mike and gcd problem 贪心

Qt+OpenGL+Cesium三件套:打造高性能3D GIS界面的5个关键技巧 本文深入探讨了如何利用Qt、OpenGL与Cesium三件套打造高性能3D GIS界面的5个核心技巧。内容涵盖多线程渲染架构设计、OpenGL状态管理与批处理优化、内存与显存高效管理、Qt UI与3D场景深度集成,以及跨平台部署与性能调优实战,旨在帮助开发者解决海量3D Tiles数据渲染的性能瓶颈,实现流畅的交互体验。 阅读详情

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

Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .

Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbersai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.

 is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

Input

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

Output

Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.

If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

Examples
input
2
1 1
output
YES
1
input
3
6 2 4
output
YES
0
input
2
1 3
output
YES
1
Note

In the first example you can simply make one move to obtain sequence [0, 2] with .

In the second example the gcd of the sequence is already greater than 1.


题意大体是根据他给定的修改方法,问最少修改多少次,使得总序列的最小公因子大于1
做法是先判断原序列最小公因子是否大于1,如果不是,则一定会改成最小公因子为偶数的序列。
为什么一定会改成最小公因子为偶数呢,可以用反证法
假设两个公因子不包括奇数k的数 a,b
经过修改后最小公因子变为奇数 k
那么也就是  (a-b)%k==0&&(a+b)%k==0
通过前式可以知道a和b是膜k同余的,a%k==b%k==x(x>=0&&x<k)
那么(a+b)%k==(2*x)%k,根据后式可知(a+b)%k==0,因为k是奇数,所以x的取值只能为0,这样和a,b互质矛盾
然后就是贪心的修改,使得序列变成全偶数
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define eps 1e-9
#define PI 3.141592653589793
#define bs 1000000007
#define bsize 256
#define MEM(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
long long a[100005];
int gcd(long long u,long long v)
{
	if(v==0)
	return u;
	return gcd(v,u%v);
}
int main()
{
	long long n,i,ans=0;
	long long u,v;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	scanf("%lld",&a[i]);
	int flog=1;
	int g=a[n-1];
	for(i=0;i<n-1;i++)
	g=gcd(g,a[i]);
	if(g!=1)
	{
		printf("YES\n0\n");
		return 0;
	}
	for(i=0;i<n-1;i++)
	{
		if(a[i]&1)
		{
			if(a[i+1]&1)
			{
				ans++;
				a[i+1]+=a[i];
			}	
			else
			{
				ans+=2;
				a[i+1]=2*a[i];
			}
		}
	}
	if(a[n-1]&1)
	ans+=2;
	printf("YES\n%lld\n",ans);
	return 0;
 }
不过在判断原序列最小公因子是不是大于1的地方马虎出了点小问题。。。。没过终测=。=



【图论】系列(一)之图论的构成 图论是研究图的数学分支,本章介绍了图论的基本构成和概念 阅读详情

相关推荐

Oracle GoldenGate (OGG) 实战:从安装到故障排查的完整避坑指南

本文提供了一份详尽的Oracle GoldenGate实战指南,涵盖从环境准备、核心进程配置到性能调优与故障排查的全过程。重点解析了OGG安装与部署中的常见陷阱,并分享了确保数据同步链路稳定运行的高级运维技巧与常见故障处理方法,帮助技术人员高效避坑。

weixin_29230649的博客 380

C. Mike and gcd problem(思维+数论+dp/贪心好题)

https://codeforces.com/problemset/problem/798/C 题意:给定一个数列,可以进行以下的操作,选相邻两个数进行x-y,x+y的操作。问最少多少次使得整个序列gcd>1; 思路: 模拟可以发现最多两次就可以出gcd==2的两个数。 而且如果原来就gcd!=1,是不会通过1次变成gcd>1的; 假设t|x,t|y--->t|ax+by;因此如果1次后gcd可以>1,原来就可以>1。因此不会发生这样事。 我们继续...

代码笔记本 413

STM32F429移植LVGL9.3.0:从源码配置到双缓冲优化实战

本文详细介绍了如何在STM32F429平台上移植LVGL 9.3.0图形库,涵盖从源码准备、工程配置、显示驱动适配到双缓冲优化等关键步骤。重点解析了利用STM32F429的硬件特性实现高效内存管理和流畅显示性能的实战技巧,为嵌入式GUI开发提供完整解决方案。

q6r7s8t9的博客 1135

Mike and gcd problem(思维)

Mike has a sequence A = [a1, a2, …, an] of length n. He considers the sequence B = [b1, b2, …, bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence i...

starlet_kiss的博客 332

泛微E9与金蝶云星空单点登录集成实战指南

本文详细介绍了泛微E9与金蝶云星空单点登录(SSO)集成的实战指南,包括前期准备、核心代码实现、常见问题排查及安全加固建议。通过集成,企业可实现一次登录全网通行,显著提升工作效率和安全性。文章特别强调了签名生成机制和跳转URL构造的关键技巧,帮助开发者避免常见错误。

weixin_30689307的博客 535

【工控基础】UVW平台踩坑

1 每个轴按顺时针或逆时针走一个距离,就是旋转。 2 图中W动,U轴V轴不动,就是X方向的平移 3 图中U轴V轴同时动,X不动,就是Y方向的平移。

songhuangong123的博客 6951

Codeforces 798C Mike and gcd problem (贪心)

题目链接 题意:给出n个数,问你能否通过下面的操作使之gcd(a1,a2,...,an)&gt;1,可以就输出YES和最少操作数,否则输出NO。操作:对于每个a[i]和a[i+1],每操作一次后有:a[i]=a[i]-a[i+1],a[i+1]=a[i]+a[i+1];题解:对于gcd(x , y)要大于1的情况,最直接想到的就是使不满足的数变成偶数使之总的gcd为2,而且对于相邻的2个数,如果...

Fushicho_XF的博客 298

在 MATLAB/Simulink 中构建一个卫星间激光通信链路的完整仿真系统

摘要: 本文通过Simulink详细演示了卫星间激光通信系统的建模过程,重点分析其在10Gbps高速率、2000km距离下的性能。系统设计涵盖PAM-4调制、自由空间信道建模(路径损耗、指向误差、多普勒效应)、APD探测及链路预算计算,并给出典型场景下18.5dB SNR、1e-9误码率的仿真结果。针对激光通信的μrad级指向精度挑战,提出AI辅助跟踪、混合RF/激光等优化策略,为未来太空高速组网提供可扩展的仿真框架。所需工具箱包括Communications Toolbox和Aerospace Toolb

xiaoheshang_123的博客 383

CF#798 C. Mike and gcd problem(思维)

C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a sequence A = [a1, a2, ..

xiaotuji的博客 947

【Zephyr电源与功耗专题】13_PMU电源驱动介绍

本文介绍的PMU芯片包含2路DCDC和6路LDO,在Zephyr实时操作系统下实现了完整的电源管理解决方案。

zero的博客 842

FPGA 学习笔记:Vivado 工程更改FPGA 型号

FPGA 学习笔记:Vivado 工程更改FPGA 型号

坚韧与专注 1万+

CodeForces798C Mike and gcd problem(思路)

题意:给你一个整数序列a,每次操作把a[i],a[i+1]删除,用a[i] - a[i + 1]和a[i] + a[i + 1]替换,问最少多少次操作可以使序列a的最大公约数大于1? 思路:显然一定可以通过操作使a的gcd大于1。首先,先算一遍gcd,如果大于1,直接输出0。否则从头开始检查,如果a[i]为偶数不用变,因为偶数的最大公约数一定大于1,如果是奇数,再看a[i + 1]的奇偶性,如果

fly_tzf的博客 384

手把手教你学基于 Linux 的 NPU 固件开发-第 10 讲:可靠性设计(RAS)——让系统坚如磐石错误检测、纠正与上报机制(ECC、Parity)

组件推荐保护机制备注HBM/DDRScrubbing必须开启,定期巡检片上 SRAMECC(权重/激活值),Parity(指令)权衡面积与可靠性计算单元ABFT或DMR(关键路径)防止计算逻辑错误片上网络CRC重传保证数据传输完整性系统软件AER 上报CE 监控自动恢复闭环管理,杜绝 SDC运维策略预测性维护Checkpoint变被动为主动核心心法(信任硬件,但必须验证。在纳米尺度下,错误是必然发生的物理现象。

MHD0815的博客 92

【解题报告】Codeforces Round #410 (Div. 2)

简略的解题报告。

TRiddle的博客 1144

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

CF-Codeforces Round #410 (Div. 2)-A~C

ACM模版很少打 CF,最近开始正儿八经的打 CF 了,以前不打是因为看英语题感觉累,但是以后这个是避免不了的,平时也需要多训练才行,所以现在开始折腾 CF 了……由于前三题都十分简单,只要看懂题,就没有什么困难的,所以题解就写在一起吧。A - Mike and palindrome描述题解只能修改一个字符,并且必须修改一个字符,然后保证回文,问,是否可行?可行输出 YES,否则 NO。这里实际上就

逐梦者 810

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Mike and gcd problem-codeforces-Round 410 Div2-C

题意给你n个数(2 ≤ n ≤ 100 000) ,A = [a1, a2, …, an] 。可以对第i个数进行操作(1 ≤ i < n),使a[i],和a[i+1]变为a[i]-a[i+1]和a[i]+a[i+1]。问你最少经过多少次操作能使gcd(a[1],…,a[n])>1。能输出YES和最小的操作数,不能输出NO。题解先判断原序列最小公因子是否大于1,如果不是,则一定会改成最小公因子为偶数的

WilliamSun0122的博客 458

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【CodeForces - 798C Mike and gcd problem】 思维+贪心

E - Mike and gcd problem   Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger t

于心有愧丶 373

C. Mike and gcd problem

C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a sequence A = [a1, a2, ..

樱花满地集于我心,楪舞纷飞祈愿相随 781

Codeforces 798C Mike and gcd problem【思维+贪心】好题!

C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a sequence A = [a1, a2, ..., an] of

mengxiang000000 1119

Mike and gcd problem

Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .     Mike wants to change his

码农的一亩三分地 358

Codeforces 798C Mike and gcd problem gcd+贪心

点击打开链接 题意:n个数,n1的最少操作次数  若初始d=gcd(a1..an)==1,肯定使用操作,则使用操作后->d|2a[i] d|2a[i+1],可以看出最后的d一定整除偶数,所以n个数必须都为偶数.  问题转化为把n个数变为偶数的最小操作次数 a[i]-a[i+1],a[i]+a[i+1],相邻两个odd需要一次操作,相邻odd,even 需要两次操作 贪心:先处理(odd

Masamiiiiii 1665

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

题目链接:Codeforces-798C-Mike and gcd problem 因为d|ad|a && d|bd|b -> d|(ax+by)d|(ax+by) d|(a−b)d|(a-b) && d|(a+b)d|(a+b) -> d|2ad|2a && d|2bd|2b所以要将 gcd(ai)=1gcd(a_i)=1 转化成 gcd(ai)=2gcd(a_i)=2 。 将奇奇变成偶

On the way 1119

codeforce 798C Mike and gcd problem (简单数学)

C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike has a sequence A = [a1, a2, ..

K键盘里的青春K 667

Codeforces 798C - Mike and gcd problem

D - Mike and gcd problemTopic: Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .

RE:X 329

C. Mike and gcd problem 数论 + 贪心

Mike has a sequence A = [a1, a2, …, an] of length n. He considers the sequence B = [b1, b2, …, bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence ...

loooooog的博客 387

Codeforces Round #382 (Div. 2)A到D题 A. Ostap and //B. Urbanization//C. Tennis Championship//D. Taxes

A. Ostap and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output On the way to Rio de Janeiro Ostap

jerans的博客 1287
上一篇: UVALive - 5088 Alice and Bob's Trip 树形dp
下一篇: Gym - 100765I Mars Stomatology 二维dp好题
jerans
博客等级 码龄10年 54粉丝 255原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值