HDU - 6025 Coprime Sequence(gcd+前缀后缀)
``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.
InputThe first line of the input contains an integer T(1≤T≤10)T(1≤T≤10), denoting the number of test cases.
In each test case, there is an integer n(3≤n≤100000)n(3≤n≤100000) in the first line, denoting the number of integers in the sequence.
Then the following line consists of nn integers a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the elements in the sequence.OutputFor 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 去掉一个值,使得剩下的值gcd最大。
典型的预处理。将每个值的前缀和后缀求出来,扫一遍每个值,ans=max(ans,gcd(前缀,后缀))
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<set>
#include<map>
#include<algorithm>
#define MAX 100005
#define INF 0x3f3f3f3f
using namespace std; int a[MAX];
int pre[MAX],sub[MAX];
int gcd(int x,int y){
if(y) return gcd(y,x%y);
return x;
}
int main()
{
int t,f,h,n,m,k,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%d",&a[]);
pre[]=a[];
for(i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=gcd(pre[i-],a[i]);
}
sub[n]=a[n];
for(i=n-;i>=;i--){
sub[i]=gcd(sub[i+],a[i]);
}
int maxx=(pre[n-]>sub[]?pre[n-]:sub[]);
for(i=;i<n;i++){
if(gcd(pre[i-],sub[i+])>maxx) maxx=gcd(pre[i-],sub[i+]);
}
printf("%d\n",maxx);
}
return ;
}
HDU - 6025 Coprime Sequence(gcd+前缀后缀)的更多相关文章
- HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)
题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...
- HDU 6025 Coprime Sequence
枚举,预处理. 预处理前缀$gcd$与后缀$gcd$,枚举删哪一个即可. #include <bits/stdc++.h> using namespace std; int T,n; ]; ...
- Coprime Sequence(前后缀GCD)
Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...
- HDU 6186 CS Course(前缀+后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. ...
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...
- Coprime Sequence (HDU 6025)前缀和与后缀和的应用
题意:给出一串数列,这串数列的gcd为1,要求取出一个数使取出后的数列gcd最大. 题解:可以通过对数列进行预处理,求出从下标为1开始的数对于前面的数的gcd(数组从下标0开始),称为前缀gcd,再以 ...
- hdu 6025 前缀 后缀 gcd
大致题意: 去掉一个元素能使这个数列的GCD最大为多少 分析: 我们求一个数列的GCD,是先求前两个元素的GCD,然后将这个GCD值在与下一个元素进行GCD运算.由此可知进行GCD运算的顺序对最终的结 ...
- HDU6025 Coprime Sequence(gcd)
HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...
- HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏
Coprime Sequence Time Limit: 2000/1000 MS (Ja ...
随机推荐
- 使用SqlDependency监听MSSQL数据库表变化通知
SqlDependency提供了这样一种机制,当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的. ...
- Complete space 完备空间与柯西序列 巴拿赫空间与完备空间 完备空间与和希尔伯特空间 封闭closed与完备性complete
http://www.gatsby.ucl.ac.uk/~gretton/coursefiles/RKHS2013_slides1.pdf RKHS: a function space with a ...
- linux-shell脚本命令之grep
版权声明: https://blog.csdn.net/zdp072/article/details/26015611 [ grep简单介绍: ] grep是用来过滤含有特定字符的行, 能使用正則表達 ...
- [IR课程笔记]Hyperlink-Induced Topic Search(HITS)
两个假设 1. 好的hub pages: 好的对某个主题的hub pages 链接许多好的这个主题的authoritative pages. 2. 好的authoritative pages: 好的对 ...
- nodejs搭建简单的websocket服务端
创建websocket服务端使用了nodejs-websocket ,首先要安装nodejs-websocket,在项目的目录下: npm install nodejs-websocket 1.搭建w ...
- 学习 Promise,掌握未来世界 JS 异步编程基础
其实想写 Promise 的使用已经很长时间了.一个是在实际编码的过程中经常用到,一个是确实有时候小伙伴们在使用时也会遇到一些问题.Promise 也确实是 ES6 中 对于写 JS 的方式,有着真正 ...
- rbx1包里机器人仿真程序的实践
git clone https://github.com/pirobot/rbx1.git 1.打开一个终端 cd ~/catkin_ws/ catkin_make source ./devel/s ...
- jumpserver v3.0
文档地址 www.php230.com/weixin1451347094.html
- 创建Django博客的数据库模型
声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 blog最主要的功能就是展示我们写的文章 ...
- JavaScript中函数的无限次运算问题
开博客有一段时间了,一直没动笔,原因是确实没看到什么值得写的内容.直到今天在司徒正美的博客里看到一个问题. http://www.cnblogs.com/rubylouvre/archive/2012 ...