题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025

Coprime Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 666    Accepted Submission(s): 336

Problem Description
Do you know what is called ``Coprime Sequence''? That is a sequence consists of n positive
integers, and the GCD (Greatest Common Divisor) of them is equal to 1.

``Coprime Sequence'' is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements.
 
Input
The first line of the input contains an integer T(1≤T≤10),
denoting the number of test cases.

In each test case, there is an integer n(3≤n≤100000) in
the first line, denoting the number of integers in the sequence.

Then the following line consists of n integers a1,a2,...,an(1≤ai≤109),
denoting the elements in the sequence.
 
Output
For each test case, print a single line containing a single integer, denoting the maximum GCD.
 
Sample Input
3
3
1 1 1
5
2 2 2 3 2
4
1 2 4 8
 
Sample Output
1
2
2
 

题解:

l[i]为前i个数的gcd, r[i]为后i个数的gcd。

假设被删除的数的下标为i, 则 删除该数后的gcd为: gcd(l[i-1], r[i+1]), 枚举i,取最大值。

学习之处:

当提到在序列里删除一段连续的数时,可以用前缀和+后缀和

例如:http://blog.csdn.net/dolfamingo/article/details/71001021

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e5+10; int n;
int a[maxn], l[maxn], r[maxn]; int gcd(int a, int b)
{
return b==0?a:(gcd(b,a%b));
} void solve()
{
scanf("%d",&n);
for(int i = 1; i<=n; i++)
scanf("%d",&a[i]); l[1] = a[1]; r[n] = a[n];
for(int i = 2; i<=n; i++)
l[i] = gcd(l[i-1], a[i]);
for(int i = n-1; i>=1; i--)
r[i] = gcd(r[i+1], a[i]); int ans = 1;
l[0] = a[2]; r[n+1] = a[n-1];
for(int i = 1; i<=n; i++)
ans = max(ans, gcd(l[i-1], r[i+1]) );
cout<<ans<<endl;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
solve();
}
return 0;
}

HDU6025 Coprime Sequence —— 前缀和 & 后缀和的更多相关文章

  1. HDU6025 Coprime Sequence(gcd)

    HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...

  2. Coprime Sequence(前后缀GCD)

    Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...

  3. HDU - 6025 Coprime Sequence(gcd+前缀后缀)

    Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, ...

  4. HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)

    题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...

  5. HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏

    Coprime Sequence                                                        Time Limit: 2000/1000 MS (Ja ...

  6. 递归算法(二)——前缀转后缀

    源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /**** ...

  7. POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)

    给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...

  8. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

  9. 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理

    一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...

随机推荐

  1. PV、PVC、StorageClass讲解

    PV.PVC.StorageClass讲解 为了方便开发人员更加容易的使用存储才出现的概念.通常我们在一个POD中定义使用存储是这样的方式,我们以hostpath类型来说: apiVersion: v ...

  2. Java 8 Streams的简单使用方法

    Java 8 Streams的简单使用方法 package JDK8Test; import java.util.ArrayList; public class Main { public stati ...

  3. 好用的Python IDLE Sublime Text 3推荐

    Sublime Text 3 下载地址为 LINK, Sublime Text 3 is currently in beta. The latest build is 3114. 参考的激活方式为输入 ...

  4. activiti自己定义流程之整合(二):使用angular js整合ueditor创建表单

    基础环境搭建完成,接下来就该正式着手代码编写了,在说代码之前.我认为有必要先说明一下activit自己定义流程的操作. 抛开自己定义的表单不谈.通过之前的了解,我们知道一个新的流程開始.是在启动流程实 ...

  5. 用ELK 实时处理搜索日志

    转载请标明原处:http://blog.csdn.net/hu948162999/article/details/50563110 本来这块业务 是放到SolrCloud上去的 , 然后 採用solr ...

  6. 辛星深入分析vim的自己主动补全功能以及vim的映射

    曾经对于vim的自己主动补全功能,都是须要的时候从网上下载点配置项,然后复制到自己的vimrc上去,自己也不知道是什么意思.结果发现搜索到的非常多自己主动补全的方式都非常另类,有的喜欢在补全大括号的时 ...

  7. ARM体系结构与编程-5

    GET通经常使用于包括定义常量的源文件. 比如:GET 2440addr.inc 用AREA定义一个段.ENTRY用于指定程序的入口点,END用于告诉汇编器源文件已经结束. 比如: AREA init ...

  8. 轻松搞定RabbitMQ(二)——工作队列之消息分发机制

    转自 http://blog.csdn.net/xiaoxian8023/article/details/48681987 上一篇博文中简单介绍了一下RabbitMQ的基础知识,并写了一个经典语言入门 ...

  9. 设置Activity进入退出动画

    http://blog.csdn.net/tenpage/article/details/7792689 http://blog.csdn.net/lnb333666/article/details/ ...

  10. EEPlat 主子表和对象引用配置实例

    本次实例以常见的订单维护,来介绍下平台内类似主子表结构的配置方法. 订单包含订单头和订单明细.订单头包含简单信息:订单编号.订单状态.客户. 交付日期.订单日期.备注等.订单明细包含:订单产品.定单数 ...