题目链接: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. Centos7源码安装MySQL5.7

    a.连接数据库报111 从一台linux远程连接另一台linux上的MySQL,出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx. ...

  2. RabbitMQ 延迟队列,消息延迟推送

    目录 应用场景 消息延迟推送的实现 测试结果 应用场景 目前常见的应用软件都有消息的延迟推送的影子,应用也极为广泛,例如: 淘宝七天自动确认收货.在我们签收商品后,物流系统会在七天后延时发送一个消息给 ...

  3. java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page pagehelper报错无法类型转化。

    报错信息: 严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request ...

  4. Deep learning with PyTorch: A 60 minute blitz _note(1) Tensors

    Tensors 1. construst matrix 2. addition 3. slice from __future__ import print_function import torch ...

  5. cookie理解

    cookie的作用域是domain本身以及domain下的所有子域名. cookie的作用域是domain本身以及domain下的所有子域名. cookie的作用域是domain本身以及domain下 ...

  6. win10 安装git

    http://jingyan.baidu.com/article/a3a3f811d4cd308da2eb8ad1.html 双击exe安装包,在弹出的安全提示中点击“允许”.   安装向导的头两步都 ...

  7. procomm plus

    procomm plus这是查看串口数据的软件.

  8. Go语言阅读小笔记,来自知呼达达关于unsafe.Pointer的分享.

    第一式 - 获得Slice和String的内存数据 func stringPointer(s string) unsafe.Pointer { p := (*reflect.StringHeader) ...

  9. Codeforces Beta Round #1 A. Theatre Square

    从今天開始.就要在Codeforces里有一个新的開始了,貌似任务非常重的说~~ Codeforces专题我将会记录全部通过的题目,事实上仅仅要通过的题目都是水题啊!. 题目大意: 依照要求计算须要多 ...

  10. persits.jpeg 水印组件

    官方下载的persits.jpeg 都须要注冊.不然就有时间限制.可今天须要个persits.jpeg 破解版安装到server上,可百度了半天也没找到.最后还是找到了. 很捧的水印组件,玩serve ...