A. Pride (emmmm练习特判的好题)
题目连接 : 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?
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.
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.
5 2 2 3 4 6
5
4 2 4 6 8
-1
3 2 6 9
4
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练习特判的好题)的更多相关文章
- hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)
Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- [HNOI2009]最小圈 分数规划 spfa判负环
[HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...
- POJ1061 青蛙的约会-拓展欧几里得
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- kuangbin_ShortPath F (POJ 3259)
判环模板题 有了上一题的经验过得很轻松 除了因为spfa还不是很熟打错了两个字母 然后debug了一小会 #include <iostream> #include <string&g ...
- 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 ...
- BZOJ刷题指南(转)
基础(65) 巨水无比(4):1214.3816:2B题:1000A+B:2462:输出10个1 模拟/枚举/暴力(15):4063傻子模拟:1968小学生暴力:1218前缀和暴力:3856读英文:4 ...
- 10-17(day2)
这次写day2的总结 T1:表达式 题面:给你一串表达式 在本题中,我们对合法表达式定义如下:1. 任何连续(至少1个)数字是合法表达式:2. 若x是合法表达式,则(x)也是合法表达式:3. 若x和y ...
- LOJ#6049. 「雅礼集训 2017 Day10」拍苍蝇(计算几何+bitset)
题面 传送门 题解 首先可以用一个矩形去套这个多边形,那么我们只要枚举这个矩形的左下角就可以枚举完所有多边形的位置了 我们先对每一个\(x\)坐标开一个\(bitset\),表示这个\(x\)坐标里哪 ...
- [题解向] CF#Global Round 1の题解(A $\to$ G)
这里是总链接\(Link\). \(A\) 题意:求\(\sum_{i=1}^{k} a_i\times b^{k-i}\)的奇偶性, \(k = \Theta(n \log n)\) --其实很容易 ...
随机推荐
- Flink on Yarn模式启动流程源代码分析
此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Flink on yarn的启动流程可以参见前面的文章 Flink on Yarn启动流程,下面主要是从源码角 ...
- E20181030-hm
conquer vt. 征服; 克服; 攻克; 打败(敌人); vi. 得胜,胜利; recur vi. 复发; 重现; 再发生; 回想; recurrence n. 复回,重现; 反复,隐现; ...
- TP5之查询那些事
1.使用 model 查询,查出的类型为 对象 $a 是一个对象,使用 $a->name 的方式来获取 对象里的属性 2.使用 db 查询,查询出的是 数组 $b 是一个数组,使用 $b['na ...
- ZOJ3164【区间dp】
题意: 有n个人,有一种关系叫做8g关系,给出m个关系,给出n个人的阵列 问你最多能拿走多少人,拿走以后相邻就是相邻了 思路: 典型的区间dp: dp[i][j] 代表 i-j 最多能去多少人: 如 ...
- [Xcode 实际操作]九、实用进阶-(4)计算两个日期间的差值
目录:[Swift]Xcode实际操作 本文将演示如何计算两个日期之间的差值. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class ...
- MyBatis源码解析(一)
<!-- mybatis文件配置,扫描所有mapper文件 --><!--SqlSessionFactoryBean的初始化参数--> <bean id="sq ...
- line-height与图文对齐 笔记
基本概念: 块:block 特点独行 内联:inline 内联块:inline-block 如果元素display属性默认值为block,则为块元素.如div p 如果元素display属性默认值为i ...
- 常用HTTP协议响应码(转载)
转载于: https://blog.csdn.net/github_36032947/article/details/78343734 HTTP响应码,也称http状态码(HTTP Status Co ...
- android studio ffmpeg简单使用 (cmake)
编译ffmpeg android studio 新建项目,勾选上 将编译好的libffmpeg.so库扔到src/main/jniLibs/armeabi下(主要这里我只编译了arm的ffmpeg的库 ...
- dzzoffice 任意文件下载漏洞分析
dzzoffice 任意文件下载 \updload\dzz\system\save.php第72行开始: elseif($_GET['do']=='move'){ $obz=trim($_ ...