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

宿舍网速跑不满?可能是PPPoE的锅!实测OpenWrt切换DHCP+深澜认证,轻松跑满校园百兆宽带 本文探讨了校园网PPPoE协议的性能瓶颈问题,并提供了通过OpenWrt路由器切换DHCP+深澜认证的技术方案,实测可显著提升网速至百兆宽带满速。文章详细介绍了配置步骤、性能对比及常见问题解决方案,帮助用户优化校园网体验。 阅读详情
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.


题意:给出一个序列 A ,我们可以将相邻两元素 a [ i ] , a [ i + 1 ] 改为 a [ i ] - a [ i + 1 ] 和 a [ i ] + a [ i + 1 ] ,问至少多少次操作后能使 gcd ( A1 ~ An) 大于1,若无法完成,则输出 NO

思路:显然我们把所有奇数修改成偶数即可满足条件,每次修改时若  a [ i ] 和 a [ i + 1 ] 同为奇数则修改一次,若一奇一偶则修改两次


#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;

int n;
int a[N],c[N];

int gcd(int n,int m)
{
    int t = 1;
    while(t)
    {
        t = n % m;
        n = m;
        m = t;
    }
    return n;
}

int main()
{
    while(scanf("%d", &n) == 1)
    {
        memset(c,0,sizeof(c));
        for(int i=0; i < n; i++)
        {
            scanf("%d",& a[i]);
        }
        int ans = 0,tmp = gcd(a[0], a[1]);
        for(int i = 0; i < n; i++)
        {
            tmp = gcd(tmp, a[i]);
            if(a[i] & 1 && !c[i])
            {
                if(a[i + 1] & 1) ans++;
                else ans += 2;
                c[i] = 1;
                c[i + 1] = 1;
            }
        }
        if(tmp != 1) printf("YES\n0\n");
        else printf("YES\n%d\n", ans);
    }
}

 
如何用Bertalign快速搞定多语言句对齐?5分钟上手教程(附避坑指南) 本文详细介绍了如何利用Bertalign工具快速实现多语言句对齐,从安装配置到实战应用的全流程指南。通过LaBSE句向量技术,Bertalign显著提升了对齐准确率,支持25种语言的任意组合,特别适合处理复杂句式。文章还提供了常见问题的解决方案和性能优化技巧,帮助用户高效完成多语言文本对齐任务。 阅读详情

相关推荐

Matt Pocock Skills — AI 编码代理技能包介绍与使用指南

《Matt Pocock Skills:AI编码代理技能包指南》 摘要: Matt Pocock Skills是一套开源AI编码代理技能包,旨在解决AI辅助开发中的四大痛点:需求理解偏差、token浪费、代码质量差和架构腐化。该技能包包含多种实用工具:/grill-with-docs用于需求对齐,/tdd实现测试驱动开发,/diagnose提供规范化调试流程,/triage管理issue生命周期。核心思想包括:通过拷问式对齐确保需求理解一致,建立共享领域语言提高沟通效率,快速反馈闭环加速开发,以及持续架构维

qq_45859163的博客 2221

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

学术类PPT制作总结

本文分享了科技类PPT的制作技巧,希望相关的信息能帮助到读者。

奇妙水果的技术博客,欢迎交流技术~ 2881

Basic Gcd Problem

Basic Gcd Problem 时间限制: 2 Sec 内存限制: 256 MB 题目描述 As a great ACMer, ZYBZYBZYB is also good at math and number theory. ZYBZYBZYB constructs a function fc(x)f_c(x)fc​(x) such that: Give some positive integer pairs (ni,ci)(n_i, c_i)(ni​,ci​), ZYB wants to kn

Amber的博客 222

避坑指南:Unity新版InputSystem在移动端的5个常见问题及解决方案

本文针对Unity新版InputSystem在移动端开发中的常见问题,提供了5个关键解决方案,包括虚拟摇杆灵敏度调节、多点触控冲突处理、按钮反馈延迟优化、手势识别与陀螺仪数据融合以及Android/iOS平台差异处理。这些实战经验帮助开发者高效解决触控适配难题,提升移动游戏体验。

soda5的博客 404

小华半导体HC32芯片开发使用说明

HC32F/L系列MCU在Keil MDK和IAR Workbench环境下通过工程样例进行软件开发。

小布的芯视角 5630

C. Mike and gcd problem

C. Mike and gcd problem题意:​ 定义一种beautiful数列,满足gcd(b1,b2,···,bn) > 1.​ 现在给出一个数列A,有一种操作:对ai操作,使得a[i] = ap[i]-a[i+1],a[i+1] = a[i]+a[i+1]​ 问最少多少次操作使得题中满足beautiful。思路:​ 数学题就多思考,有公式的情况下向公式思考。根据操作的方法

Since_natural_ran 409

实现画布拖拽算子排布,通过flink实现算子编排计算,如何实现?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1071

C - Mike and gcd problem /* CodeForces - 798C */

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 sequenceA = [a1, a2, ....

b123103603070256的博客 208

PVE虚拟机运行不流畅,出现卡顿。

🏆本文收录于《优快云问答解惑-专业版》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!

**My Coding Family** 3062

FISCO BCOS(八)——— 一键部署 WeBase

环境 版本 Java Oracle JDK 8 至 14 MySQL MySQL-5.6及以上 Python Python3.6及以上 PyMySQL 1、获取部署安装包: wget https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/WeBASE/releases/download/v1.5.4/webase-deploy.zip 2、解压安装包: unzip webase-deploy.zip 3、进入目录: c...

林中有神君 3607

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

题目链接:Codeforces-798C-Mike and gcd problem  因为d|ad|a &amp;&amp; d|bd|b -&gt; d|(ax+by)d|(ax+by)  d|(a−b)d|(a−b) &amp;&amp; d|(a+b)d|(a+b) -&gt; d|2ad|2a &amp;&amp; d|2bd|2b 所以要将 gcd(ai)=1gcd(ai)=1 转化成 ...

c_czl的博客 218

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

B.Basic Gcd Problem(质因数分解)

B.Basic Gcd Problem(质因数分解) 思路:考虑ccc的贡献次数,我们最后的答案肯定是:ccntc^{cnt}ccnt形式,所以我们只需求出cntcntcnt。 ep:n=16,c=5ep:n=16,c=5ep:n=16,c=5。 显然fc(16)=c×max{fc(i)},i∈{1,2,4,8}f_c(16)=c\times max\{f_c(i)\},i\in\{1,2,4,8\}fc​(16)=c×max{fc​(i)},i∈{1,2,4,8}。 fc(1)=1f_c(1)=1fc​(

Harris-H的博客 961

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

2020牛客暑期多校训练营(第四场)Basic Gcd Problem

题目描述 分析 看到这道题的主体是一个递推式,那么绝不可能只是简单的套公式 那么就要进行优化 再仔细看一看,发现gcd,c*f(n)(n为某数)。 那么gcd,快速幂,素数筛先打好。 ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}//gcd ll ksm(ll x,ll y)//快速幂拼音首字母 { ll ret=1; while(y) { if(y&1) ret=ret*x%mod; x=x*x%

qq_46070004的博客 231

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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

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

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

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

[CF798C] Mike and gcd problem(规律,gcd)

题目链接:http://codeforces.com/contest/798/problem/C 题意:给n个数,和一种操作:把a(i)和a(i+1)变成a(i)-a(i+1)和a(i)+a(i+1)。问能不能把整个数列变成gcd>1,能的话输出最少步骤。 贪心地认为假如需要这种操作,那么gcd为2是最好的。 进行操作无非四种情况:奇奇 奇偶 偶奇 偶偶。奇偶和偶奇可以看成一种情况,而...

weixin_30470643的博客 330

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

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

C. Mike and gcd problem-递推或者贪心

http://codeforces.com/problemset/problem/798/C 给定你一个数列,你可以进行以下的操作, a,b –a-b,a+b,让这些数的最大公约数大于1,问你最少的操作是多少 因为d|a && d|b -> d|(ax+by) d|(a−b) && d|(a+b) -> d|2a && d|2b 所以要将 gcd(ai)=1 转化成 gcd(ai)=2

天与云与山与水 383

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

【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 【题解】...

168

cf 798c Mike and gcd problem

. 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, ...

mwhybmj的博客 426
上一篇: HDU 1231 最大连续子序列(DP)
下一篇: CF#798 B. Mike and strings(思维)
信鸦
博客等级 码龄10年 10粉丝 133原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值