HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接: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
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.
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.
3
3
1 1 1
5
2 2 2 3 2
4
1 2 4 8
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 —— 前缀和 & 后缀和的更多相关文章
- HDU6025 Coprime Sequence(gcd)
HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...
- Coprime Sequence(前后缀GCD)
Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...
- HDU - 6025 Coprime Sequence(gcd+前缀后缀)
Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, ...
- HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)
题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...
- HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏
Coprime Sequence Time Limit: 2000/1000 MS (Ja ...
- 递归算法(二)——前缀转后缀
源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /**** ...
- POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...
- 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树
另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...
- 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理
一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...
随机推荐
- SQLite数据库中rowid使用
SQLite数据库中rowid使用 SQLite中每个表都默认包含一个隐藏列rowid,使用WITHOUT ROWID定义的表除外.通常情况下,rowid可以唯一的标记表中的每个记录.表中插入的第 ...
- HttpClient的Post请求数据
最近在项目中需要添加Post请求数据,以前的Get请求是使用JDK自带的URLConnection.在项目组人员的推荐下,开始使用HttpClient. HttpClient简介: HttpClien ...
- input 对伪元素(:before :after)的支持情况
最近做一个自定义视觉效果的Switch组件,用到了 input:radio 和 label,并在label里用伪元素 :before 模拟状态的切换效果. 但是同事评审的时候说可以不用label,直接 ...
- ios 6.0模拟器页面调出pop窗口消失后无法使用键盘
ios 6模拟器上,点击事件调用出pop窗口,这个窗口新创建了window,在pop窗口消失的函数中使用了makeKeyWindow,这个是将要显示的window放到最前端.发现 屏蔽这个方法后可以了 ...
- hdu1034 简单模拟
这里开一个二维数组.num[105][2]; 我也不知道N有多少,随便开的, 那么这里num[i][0] 表示当前 第 i 个人拥有的糖果数,num[i][1]表示他上面一个人分给他的糖果数.详 ...
- C结构体之位域(位段)(转)
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一种数据结构 ...
- html嵌套规则
本人半路出家的 今天学习js的时候写了一个a嵌套a标签结果js报错 一直找不到原因 专门找了一下html嵌套规则看了一下 1.块级元素 一般用来搭建网站架构.布局.承载内容……它包括以下这些标签: ...
- 验证loadrunner对Ajax内容的校验
前一阵和开发的同事一起測试某个系统的性能.此系统是发送Ajax请求到后台,再调用第三方的某项服务. 第三方服务的性能由不得我们控制.因此开发者做了一下改进.超时则直接返回. 于是在loadrunner ...
- Java IO 类
IO包中绝大部分的类都是由以下四个类直接或间接继承来的InputStream OutputStream Reader 还有Writer 其中InputStream和OutputStream代表输入流和 ...
- 关于提高沟通能力的书单 | 章鱼书单zz
上周推荐了一份关于提高写作能力的书单,这周,我们来聊聊沟通能力. 在现代社会,沟通能力变得越来越重要.人与人之间的社交渠道越来越丰富,工作中的协同合作也越来越普遍.我们要沟通的人越来越多,节奏越来越快 ...