HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)
题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大。
分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→,队友这样解读题目导致我们队的思路完全想歪了,当时真的脑子发懵,为啥没再读一遍题= =
1、求出一个数列的gcd跟计算顺序没关系。
2、如果求去掉下标为i的数字后整个数列的gcd,直接将该数字前的所有数字的gcd(prefix[i-1])和该数字后所有数字的gcd(suffix[i+1])再求一下gcd就好了。
3、预处理前缀gcd和后缀gcd即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int prefix[MAXN];
int suffix[MAXN];
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(prefix, 0, sizeof prefix);
memset(suffix, 0, sizeof suffix);
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
prefix[0] = a[0];
for(int i = 1; i < n; ++i){
prefix[i] = gcd(prefix[i - 1], a[i]);
}
suffix[n - 1] = a[n - 1];
for(int i = n - 2; i >= 0; --i){
suffix[i] = gcd(suffix[i + 1], a[i]);
}
int ans = max(suffix[1], prefix[n - 2]);
for(int i = 1; i < n - 1; ++i){
ans = max(ans, gcd(prefix[i - 1], suffix[i + 1]));
}
printf("%d\n", ans);
}
return 0;
}
HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)的更多相关文章
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...
- HDU - 6025 Coprime Sequence(gcd+前缀后缀)
Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, ...
- 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$与后缀$gcd$,枚举删哪一个即可. #include <bits/stdc++.h> using namespace std; int T,n; ]; ...
- A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)
Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...
- 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\) 即为答案. ...
- 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】
C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...
随机推荐
- 转:Nginx 性能优化有这篇就够了!
目录: https://mp.weixin.qq.com/s/YoZDzY4Tmj8HpQkSgnZLvA 1.Nginx运行工作进程数量 Nginx运行工作进程个数一般设置CPU的核心或者核心数x2 ...
- 如何书写高效的MySQL查询?
How to write efficient MySQL query statements WHERE子句中的书写注意事项 模糊查询(like)时需要注意的事项 索引 字段类型 表连接时的注意事项 其 ...
- 大数据萌新的Python学习之路(二)
笔记内容: 一.模块 Python越来越被广大程序员使用,越来越火爆的原因是因为Python有非常丰富和强大标准库和第三方库,几乎可以实现你所想要实现的任何功能,并且都有相应的Python库支持,比如 ...
- JSP上传图片程序
1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO ...
- Hive的原理和基本用法
一.Hive的概述 1.Hive的定义 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL进行数据读取.写入和管理. 2.Hive的架构图 hive ...
- spring mvc 的@PathVariable对应中文乱码解决办法
参考:https://www.aliyun.com/jiaocheng/800577.html
- 视频编解码 基本概念:GOP
前言 产品开发要求添加视频剪辑功能,翻阅有关的文档,查到了GOP(group of pictures)这个概念. 解析 GOP说白了就是两个I帧之间的间隔.比较说GOP为120,如果是720p60的话 ...
- Windows驱动开发-DeviceIoControl函数参数dwIoControlCode
函数语法 BOOL DeviceIoControl( HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBuffer ...
- k8s node断电重启
kubernetes断电重启 导致部分pod无法删除 dashboard上处于黄色 kubectl get处于terminate 状态 kubectl delete报错: An error occur ...
- 第1课 VMware的NSX全面落地软件定义网络SDN
SDN的定义: 即软件定义网络(Software Defined Network)的缩写,它是一种基于网络架构的创新,一种在已存在物理传输网络之上的抽象形态,它是一种体系结构,它是众多网络虚拟化技术中 ...