题目链接: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. Unity3D Shader 入门之简单案例的实现(通过法线实现颜色变化)

    在没有接触Unity3D  Shader 之前,总感觉shader特别神奇,因为听说是对渲染流水线进行编程,就是对GPU进行编程.听着特别高大上.这不,最近刚刚接触Shader,学了几个小案例,然后本 ...

  2. k8s的使用入门

    1.kubectl命令就是apiserver的客户端工具,可以实现对nodes资源的增删改查. # 描述一个节点的信息 kubectl describe node k8s-node1 # 查看k8s集 ...

  3. CountDownLatch、CyclicBarrier、Samephore浅谈三大机制

    CountDownLatch.CyclieBarrier与SamePhore都可用来控制线程的执行,那么他们之间有什么区别呢 CountDownLatch CountDowenlatch可以看成一个线 ...

  4. String,StringBuffer,StringBuilder源码分析

    1.类结构 String Diagrams StringBuffer Diagrams StringBuilder Diagrams 通过以上Diagrams可以看出,String,StringBuf ...

  5. 在DevExpress GridControl中添加进度条控件 z

    首先可以使用 DevExpress GridControl 自带的进度条控件. 但是我要用一个方法来设置所有的单元格进度,而不是每个单元格都要设置一遍,同时我想要根据进度值不同,进度条显示不同的颜色. ...

  6. iOS -- SKPhysicsJointSpring类

    SKPhysicsJointSpring类 继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架  /System/Library ...

  7. Host Controller transport layer and AMPs

    The logical Host Controller Interface does not consider multiplexing/routing over the Host Controlle ...

  8. Linux以下基于TCP多线程聊天室(server)

    接上篇博文,本文是server端的实现,主要实现的功能,就是现实client的连接.转发client发送的消息.以及client掉线提示等功能,同一时候能够在这这上面扩展和TCP以及线程相关的功能木块 ...

  9. javascript原生调用摄像头

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  10. Phpstorm 放大字体的快捷键是什么?

      这个功能需要设置才能使用: 步骤:control+shift+A功能可以搜索对应功能 输入mouse 设置Change font size(Zoom) ...的按钮打开,然后就可以通过 ctrl+ ...