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() { ] ...
随机推荐
- iOS -- SKTextureAtlas类
SKTextureAtlas类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKi ...
- Android自己定义组件系列【6】——进阶实践(3)
上一篇<Android自己定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计 ...
- K-L变换和 主成分分析PCA
一.K-L变换 说PCA的话,必须先介绍一下K-L变换了. K-L变换是Karhunen-Loeve变换的简称,是一种特殊的正交变换.它是建立在统计特性基础上的一种变换,有的文献也称其为霍特林(Hot ...
- poj 2932 Coneology (扫描线)
题意 平面上有N个两两不相交的圆,求全部最外层的,即不被其它圆包括的圆的个数并输出 思路 挑战程序竞赛P259页 代码 /* ************************************* ...
- C#网络编程系列文章(一)之Socket实现异步TCPserver
原创性声明 本文作者:小竹zz 本文地址http://blog.csdn.net/zhujunxxxxx/article/details/44258719 转载请注明出处 文章系列文件夹 C#网络编程 ...
- 文本聚类——Kmeans
上两篇文章分别用朴素贝叶斯算法和KNN算法对newgroup文本进行了分类測试.本文使用Kmeans算法对文本进行聚类. 1.文本预处理 文本预处理在前面两本文章中已经介绍,此处(略). 2.文本向量 ...
- WeX5开发指南
WeX5入门.UI2开发.App开发.服务端开发.扩展资料学习. 1 新手入门 1.1 运行WeX5的demo(视频) 1.2 App开发.调试.打包部署完整过程(视频) 1.3 创建第一个应用(视频 ...
- spl_autoload_register的使用
class Loader{ static function loadClass($class) { $class = $class.'.php'; if(file_exists($class)) { ...
- Swift中的switch 和 do while
switch后面的()能够省略 OC中的switch假设没有break就会穿透(依次运行),在Swift中不会穿透(可理解默认就有break) OC中入股要在case中定义变量,必要要加上{}确定作用 ...
- css实现轮播效果图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...