题目连接 : 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. Perfect项目快速上手

    1.在您从Swift.org(英文版)完成Swift 4.0 toolchain工具集安装之后,请打开一个命令行终端并输入命令 swift --version Apple Swift version ...

  2. HDU4255【BFS】

    题意: 给你一个矩阵,矩阵里是的数是这么安排的,然后给你两个数,让你求这两个数的最短距离,素数不能去: 思路: 预处理一下素数表,矩阵,然后找一下起点和终点的坐标,跑一下BFS就好了: #includ ...

  3. android调用其他apk的activity

    <img src="https://img-blog.csdn.net/20160322114625025" alt="" />启动另一个apk的工 ...

  4. button 获取 cell

        - (void)cellBtnClicked:(id)sender event:(id)event {     NSSet *touches =[event allTouches];      ...

  5. mysql之SQL入门与提升(四)——终结篇,函数

    一.SQL Aggregate (聚合)函数 SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. AVG() - 返回平均值 COUNT() - 返回行数 FIRST() - 返回 ...

  6. 第二篇 HTML5打包发布IOS APP之苹果开发者账号申请流程

    打包技术转移到了公众号

  7. w3c网址和标准化过程

  8. 213. 打家劫舍 II

    你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...

  9. Python-4-设置字符串的格式字符串

    字符串是不可变的,所有元素赋值和切片赋值都是非法的   1.替换字段名 可以按顺序和名称匹配 >>> "{foo} {} {bar} {}".format(1, ...

  10. MySql中查询语句实现分页功能

    import java.util.*;import java.sql.*; public class FruitDao {    private Connection conn;    private ...