CodeForces - 892C Pride (n个数,求相邻两个数的最大公约数替换其中一个数,看要操作几次使得n个数全部变成1)

【求问帖】数组相邻元素最大公约数不断替换直到所有元素为1 - 最少步骤数 给一组序列,求其中相邻两个元素之间的最大公约数。一直不停求下去直到所有元素被替换为1. 如果实在不能全部替换,则返回-1 输入的第一行为元素个数 n ,第二行为序列arr Example1 Input: 4 4 1 3 5 Output 3 Example2: Input: 5 10 20 30 40 50 Output: -1 上面的题目是今天模拟考的一道算法题,限时半小时。 思路: 很明显如果序列里出现1,这个序列就一定能全部转为1,因为1和任何数的公约数都为1。 那么最先设立的一个. 阅读详情

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.

What is the minimum number of operations you need to make all of the elements equal to 1?

Input

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.

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

Output

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

Example
Input
5
2 2 3 4 6
Output
5
Input
4
2 4 6 8
Output
-1
Input
3
2 6 9
Output
4
Note

In the first sample you can turn all numbers to 1 using the following 5 moves:

  • [2, 2, 3, 4, 6].
  • [2, 1, 3, 4, 6]
  • [2, 1, 3, 1, 6]
  • [2, 1, 1, 1, 6]
  • [1, 1, 1, 1, 6]
  • [1, 1, 1, 1, 1]

We can prove that in this case it is not possible to make all numbers one using less than 5 moves.


题意 :给你一个序列,每次可以选两个相邻的数,取最大公约数,然后将最大公约数替换其中一个数。问最少操作几次可以将所有数都变为1。

分析:分两种情况 (1) 输入的序列中有 1,看有多少个1,若有num个1,那么直接输出n-num就行了;(2)就是

给出的序列中没有1,那么,看最先找到两个数的最大公约数,用了几次;要如何去找

在这解释一下,我在代码中的我用的方法:问题一:为么外层循环为n-1,问题二:为什么内层循环为n-i,问题三:为什么找相邻的两个数的最大公约数要赋予前一个 问题四:还当做操作一次;

问题一:为什么外层循环为n-1,要是n的数的最大公约数都不为1,那么这个序列就不会为1了,求n的数的最大公约数最多需要n-1次;

问题二:为什么内层循环为n-i;举个例子,求4个数的最大公约数,操作一次后(求任意相邻的两个数的最大公约数代替其中一个数),只需让这两个数的最大公约数,和其他数求最大公约数,这时候就剩3个数求最大公约数了

问题三:首先明确找的是相邻两个数的最大公约数,若相邻两个数的最大公约数等于1了就结束了,若不等于1,替换其中一个,在和相邻数求gcd,对于一个数来说,它被替换成 和左边的数的gcd,或和右边数的gcd都一样,举个例子:2,6,9  任何相邻两个数的gcd都不为1,看6这个数的位置,它可以被替换成和2的gcd,再和9求gcd,或被替换成和9的gcd,再和2求gcd,你看看这两种情况的结果是一样吧;

问题四:为什么找相邻的两个数的最大公约数要赋予前一个 ,还当做操作一次; 为什么要当做一次 首先要明确

gcd(gcd(2,6),9) == gcd(gcd(2.6),gcd(6,9)); 为什么 内层循环要循环遍历;把每一个数都和相邻的求一下gcd,看看那个数和相邻的数 更快找到1;

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 2010
int n;
int a[Max];

int gcd(int c,int b)&
{
	if(b==0)
		return c;
	return gcd(b,c%b);
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		int num = 0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]==1) num ++;
		}
		if(num!=0)
		{
			printf("%d\n",n-num);
			continue;
		}
		int flag = 0;
		for(i=1;i<=n-1;i++)
		{
			for(j=0;j<n-i;j++)
			{
				a[j] = gcd(a[j],a[j+1]);
				if(a[j]==1)
				{
					printf("%d\n",i+n-1);
					flag = 1;
					break;
				} 
			}
			if(flag)
				break;
		}
		if(!flag)
			printf("-1\n");
	}
	return 0;
}



Windows下Python安装geopandas全攻略:解决GDAL依赖问题的5个关键步骤 本文详细解析了在Windows系统下安装Python地理空间分析库geopandas时,如何有效解决其核心依赖GDAL的编译难题。文章提供了五个关键步骤,包括理解依赖关系、获取预编译wheel文件、顺序安装及环境验证,旨在帮助用户绕过复杂的C++编译环境,顺利完成geopandas的安装部署。 阅读详情

相关推荐

基于gem5模拟trace的cache模拟器的实现

在做计算机体系结构的时候,模拟器是一个重要且又有效的工具。其中gem5集成了gem和m5的优点,使用起来比较简单和方便。其中包括了se模式和fs模式,se模式是在gem5运行我们已经编译好的程序,可以获取cache,cpu状态等数据,fs模式下可以模拟操作系统,我们可以将我们自己修改的linux内核加载到哥们上运行。下面介绍了使用gem5的se模式,运行编译好的ARM可执行文件。获取访存的trac

gaoxiang的专栏 8897

最大GCD ( 思维 + 数学 )

题目链接 解题报告: 显然,最优策略是我们只在区间{l,r}中找出一个元素与{ai(l<=i<=r)}使得gcd(ai,x)最大化。 对于数组中的每个数我们可以用其因子记录出现的位置;询问时对 x 因子记录的位置中查找是是否存在在区间[ l, r ]内的。 对于每个数的因子我们可以预处理出来。 #define first f #define second s #define ll l...

qhq889的博客 643

基于PID的定速巡航控制系统设计【MATLAB源码+Word文档】

本文详细介绍了定速巡航控制是一种车辆驾驶辅助系统,它能让驾驶者在高速或长途驾驶时,设定一个固定的车速,然后车辆会自动维持这个速度行驶,无需驾驶者持续踩油门。这大大减轻了驾驶者的疲劳,同时也提高了行驶的安全性。PID定速巡航控制系统是一种利用比例(Proportional)、积分(Integral)和微分(Derivative)控制算法来实现车辆定速巡航的先进系统。这种系统能够根据设定的速度,自动调整油门开度或制动力度,使车辆保持恒定的速度行驶。这种控制系统具有响应速度快、控制精度高、稳定性好等优点。

weixin_761237174的博客 1672

C. Ehab and a 2-operation task

题目链接 You’re given an array a of length n. You can perform the following operations on it: choose an index i (1≤i≤n), an integer x (0≤x≤106), and replace aj with aj+x for all (1≤j≤i), which means add x...

王小平 307

当windows安装git时出现以下错误提示

Windows安装Git时出现多个系统配置设置失败错误(exit code 4294967295),提示包括无法设置textconv、LFS过滤器和SSL等配置项。关键问题在于scalar.exe版本不兼容,系统提示"该版本的scalar.exe与你运行的Windows版本不兼容"。解决方案是检查程序版本是否正确,下载与Windows系统兼容的正确版本重新安装即可。

沧澜的博客 1884

Ansys Lumerical + HFSS + optiSLang 三剑客:手把手教你优化行波MZM调制器(附完整参数配置)

本文详细介绍了如何利用Ansys Lumerical、HFSS和optiSLang三款工具构建自动化协同设计流程,以优化行波马赫曾德尔调制器(TW-MZM)的性能。通过参数化仿真、流程集成与元模型技术,该方案能高效解决多物理场耦合挑战,实现调制带宽、驱动电压等多目标的全局优化,显著提升光子集成芯片的设计效率与性能潜力。

weixin_29251131的博客 595

算法面试题(2)

1、将一整数逆序后放入一数组中(要求递归实现) void convert(int *result, int n) {  if(n>=10)   convert(result+1, n/10);  *result = n%10; } int main(int argc, char* argv[]) {  int n = 123456789, result[20]={};  convert(resul

beijirose的专栏 350

Figma汉化教程

Figma汉化教程

cc蒲公英的博客 1万+

思维题

1.引入库 代码如下(示例): import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ssl ssl._create_default_https_context = ssl._create_unverified_context 2.读入数据 代码如...

weixin_52914088的博客 242

2021.07.05【R语言】丨预测lncRNA并绘制venn图

目录摘要环境与方法依赖工具预测工具绘制工具使用命令预测lncRNA统计noncoding_ID绘制Venn图结果展示总结 摘要 接到一个单样品测lncRNA数据的项目,正好拿来练练手,梳理一下lncRNA流程。这里记录预测lncRNA并生成venn图的过程 环境与方法 R version 3.6.3 (2020-02-29) 依赖工具 预测工具 CPC2 CNCI Pfam CPAT 绘制工具 R:VennDiagram 使用命令 预测lncRNA CPC2 #更换env conda activate py

2040

2026年【江苏“信息与未来”编程思维】真题及题解(T6:桌面游戏)

2026年【江苏“信息与未来”编程思维】真题及题解(T6:桌面游戏)

王老师青少年编程 550

CodeForces - 892C Pride GCD

CodeForces - 892C Pride GCD 题意:给你一个数列,有操作:对于相邻两个数,可以替换其中一个数为这两个数的gcd。求多少次操作后该数列全为1,不存在输出-1。 题解:可以发现,①只要数列中出现了1,那么对于1相邻的数操作,将相邻的数变为1即可(操作次数是数列中剩下不为1的数)。②数列中不存在1,需要找出1(可能需要多次操作才会出现1),那么对于原数列每个相邻的数求一次gcd,看是否出现了1。若没出现那么对于gcd再求一次gcd,知道只剩一个gcd(操作次数是反复求gcd至出现1的次数

ACZone 431

【数字IC基础】DFT(Design For Test)可测性设计

【数字IC基础】DFT(Design For Test)可测性设计

努力,加油每一天鸭~ 3477

Codeforces 892 C. Pride (枚举)

Description You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where

小坏蛋_千千 1125

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

CodeForces - 892C Pride

题意:给你一个长度为n的数组,每次可以进行一种操作把第i个数和第i+1个数替换为第i个数或者第i+1个数的gcd,问最少多少步能够使得序列全部变成1. 思路:这道题的只要数组里面有一个1 ,操作就变得简单了。如果只有一个1那么只要再执行n - 1步操作就可以全变成1,i个1就执行n-i步操作,就可以了,如果给的初始数组没有1那么我们就化出一个1来再加上n - 1,考虑每一段区间i ~ j(i &...

OOOak 273

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

CodeForces - 892 C - Pride【GCD思维】

ou have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes th

王小二的博客 528

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

codeforces 892C Pride(gcd)

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes t

henuzxy 376

算法:数组例题-python

题目归纳整理-数组

Matt_sh的博客 757

CodeForces - 892C(gcd)

PrideYou have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd deno

siyu 857

CodeForces - 891A-Pride (思维+暴力)

CodeForces - 891A-Pride You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x...

weixin_43179892的博客 386

CodeForces - 892C Pride

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denote

zitian246的博客 265

Pride -codeforces 892C

题目   You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd...

baidu_41907100的博客 353

Codeforces892C Pride

You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes ...

Sirius_han的博客 258

Codeforces-891A Pride(GCD&&思维)

A. Pride time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You have an array a with length n, you can perform operations. Each operation is

辞树 512

[codeforces 1350C] Orac and LCM 最大公约数+最小公倍数+公式推导

Codeforces Round #641 (Div. 2) 参与排名人数11937 [codeforces 1350B] Orac and Models 约数+深搜dfs 总目录详见https://blog.csdn.net/mrcrack/article/details/103564004 在线测评地址http://codeforces.com/contest/1350/problem/C Problem Lang Verdict Time Memory ...

mrcrack的博客 1639

数论(多个数gcd) CodeForces757B Bash's Big Day

B. Bash's Big Day time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Bash has set out on a journey to become the greates

PiKa__Qiu的博客 903
上一篇: UVa 1025 (DAG 上的动态规划,有固定终点的最短时间,逆推法)
下一篇: CodeForces - 894B Ralph And His Magic Field(给出n*m的格子 和 k为1或-1,往里填1 或 -1,使得每一行和每一列都和k相等)
obsorb_knowledge
博客等级 码龄9年 38粉丝 213原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址