题目连接 : http://codeforces.com/problemset/problem/891/A

          

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.

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

      题目大意:

       输入一个n,代表有n(1<=n<=2000)个数,输入n个数a1,a2,a3......an(1<=ai<=10^9),接下来你可以进行一种操作,把相邻两个数进行gcd(最大公约数)然后把得到的数附给这两个数中的其中一个

     问最少需要多少次操作可以把全部n个数都变成 1 。如果不可以的话输出 ‘ -1 ’。

    个人想法 : 一开始思路爆炸,以为操作次数基本为   n   ,  n+1   , n + 2 这三种结果发现wa 3 看了数据发现2000个数操作的3000多次 , 看的我这个难受 , 现在我还不好证明为什么能操作这么多次。

          正确思路 : 开始扫一遍看看相邻两个数能不能直接获得gcd为1如果行得通结果为n(前提是n个数全部不为1)不行的话继续,从1~n 挨个算gcd得到的gcd重新附给那两个数,

    例如gg=gcd(x[1],x[2]),x[1]=gg,x[2]=gg,(前提是x[1]!=x[2]!=gg)之后次数加1为什么加 1 呢,因为我们只是考虑每种情况实际上我们找的只是其中之一,所以是加一,之后反复,反复直到得到一个1

    或者所有的数都相同但不为 1 ,所有数都相同但不为 1 是不可能出现 1  的输出 -1 。 继而还有好多好多特判(考虑问题全面一点)。

    AC代码:

    

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
int x[];
int gcd(int a,int b){
int maxx=max(a,b);
int minn=min(a,b);
if(maxx%minn==){
return minn;
}
else{
return gcd(maxx%minn,minn);
}
}
int main()
{
int n;
scanf("%d",&n);
int yi=;
for(int i=;i<n;i++){
scanf("%d",&x[i]);
if(x[i]==){
yi++;
}
}
if(n==&&x[]==){
printf("0\n");
return ;
}
int ci=;
for(int i=;i<n-;i++){
if(gcd(x[i],x[i+])==){
printf("%d\n",n-yi);
return ;
}
}
while(){
for(int i=;i<n-;i++){
int gg = gcd(x[i],x[i+]);
if(gg==){
printf("%d\n",ci+n);
return ;
}
if(gg!=x[i]||gg!=x[i+]){
ci++;
x[i]=gg;
x[i+]=gg;
}
}
int flag=;
for(int i=;i<n;i++){
if(x[i]!=x[]){
flag=;
break;
}
}
if(flag==){
printf("-1\n");
return ;
}
}
return ;
}

A. Pride (emmmm练习特判的好题)的更多相关文章

  1. hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)

    Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  2. [HNOI2009]最小圈 分数规划 spfa判负环

    [HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...

  3. POJ1061 青蛙的约会-拓展欧几里得

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

  4. kuangbin_ShortPath F (POJ 3259)

    判环模板题 有了上一题的经验过得很轻松 除了因为spfa还不是很熟打错了两个字母 然后debug了一小会 #include <iostream> #include <string&g ...

  5. AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

    A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  6. BZOJ刷题指南(转)

    基础(65) 巨水无比(4):1214.3816:2B题:1000A+B:2462:输出10个1 模拟/枚举/暴力(15):4063傻子模拟:1968小学生暴力:1218前缀和暴力:3856读英文:4 ...

  7. 10-17(day2)

    这次写day2的总结 T1:表达式 题面:给你一串表达式 在本题中,我们对合法表达式定义如下:1. 任何连续(至少1个)数字是合法表达式:2. 若x是合法表达式,则(x)也是合法表达式:3. 若x和y ...

  8. LOJ#6049. 「雅礼集训 2017 Day10」拍苍蝇(计算几何+bitset)

    题面 传送门 题解 首先可以用一个矩形去套这个多边形,那么我们只要枚举这个矩形的左下角就可以枚举完所有多边形的位置了 我们先对每一个\(x\)坐标开一个\(bitset\),表示这个\(x\)坐标里哪 ...

  9. [题解向] CF#Global Round 1の题解(A $\to$ G)

    这里是总链接\(Link\). \(A\) 题意:求\(\sum_{i=1}^{k} a_i\times b^{k-i}\)的奇偶性, \(k = \Theta(n \log n)\) --其实很容易 ...

随机推荐

  1. Android-毛笔的探索与开发

     前言 这篇文章主要是关于移动端毛笔的开发,在平板上有着书写毛笔字贴的效果. 介绍关于毛笔的算法思路. 项目github地址 算法思路分析 曲线拟合算法 利用曲线拟合算法增加虚拟的点,使得笔迹更加光滑 ...

  2. iOS 中 常用的第三方库

    现在对于我们 iOS 开发来说,基本上说不可能不使用第三方轮子啦,毕竟没那么多时间,而且自己造的轮子往往想着成为上图中的最后一个,结果却成了上图中第二个或第一个啦,当然大公司另当别论.下面我从之前用过 ...

  3. WPF Set connectionId threw an exception异常 以及重复dll的问题

    1.DataOutputWPF 在显示norlib.Basic.UserConfigControl时 抛出异常 xmlparsingException : WPF Set connectionId t ...

  4. 简单实现TabBar的自定义

    StackoverFlow上看到的,通过继承UITabBarController创建自定义TabBarController.在原有TabBar的基础上添加一个背景层,在其基础上增加三个自定义按钮,通过 ...

  5. 两段代码实现vue路由懒加载

    const Foo = () => import('./Foo.vue') const router = new VueRouter({ routes: [ { path: '/foo', co ...

  6. JMeter博客系列:JMeter BeanShell示例

    1.简介 Apache JMeter是一个基于Java的开源工具,使您可以在应用程序上执行功能,负载,性能和回归测试.应用程序可以在Web服务器上运行,也可以是独立的.它支持在包含静态和动态资源的客户 ...

  7. GYM 101673F(树计数)

    树上每个割点计算一下各个size的组合相乘再相加为第一问答案,取最大的:再把本答案中最大的两个size相乘减掉,为第二问答案. const int maxn = 1e4 + 5; int n, siz ...

  8. 转 PHP函数---$_Get()和$_Post()的用法

    一.$_Get()和$_Post()函数是用来传值的,即对应两种提交表单的方法,get和post. 二.$_Get方法 (1)获取通过URL的传值 Example 1 新建两个PHP文件,1.php, ...

  9. 使用express+mongoDB搭建多人博客 学习(1) 安装blog工程

    一.安装 1.安装express npm install -g expressnpm install -g express-generator 2.用ejs做模板,新建blog工程express -e ...

  10. TDH-search常用命令

    一.指令部分:1.search管理界面地址: http://172.20.230.110:9200/_plugin/head/ 2.集群状态查看命令: curl -XGET 'localhost:92 ...