题意:去除数列中的一个数字,使去除后数列中所有数字的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)的更多相关文章

  1. HDU6025 Coprime Sequence —— 前缀和 & 后缀和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...

  2. HDU - 6025 Coprime Sequence(gcd+前缀后缀)

    Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, ...

  3. Coprime Sequence(前后缀GCD)

    Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...

  4. HDU 6025 Coprime Sequence

    枚举,预处理. 预处理前缀$gcd$与后缀$gcd$,枚举删哪一个即可. #include <bits/stdc++.h> using namespace std; int T,n; ]; ...

  5. A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)

    Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...

  6. Coprime Sequence (HDU 6025)前缀和与后缀和的应用

    题意:给出一串数列,这串数列的gcd为1,要求取出一个数使取出后的数列gcd最大. 题解:可以通过对数列进行预处理,求出从下标为1开始的数对于前面的数的gcd(数组从下标0开始),称为前缀gcd,再以 ...

  7. hdu 6025 前缀 后缀 gcd

    大致题意: 去掉一个元素能使这个数列的GCD最大为多少 分析: 我们求一个数列的GCD,是先求前两个元素的GCD,然后将这个GCD值在与下一个元素进行GCD运算.由此可知进行GCD运算的顺序对最终的结 ...

  8. HDU6025 Coprime Sequence(gcd)

    HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...

  9. 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】

    C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...

随机推荐

  1. 清除浮动(overflow、clear、:after等方法)

    元素在浮动之后有三个重要的特点: 脱离文档流. 向左/向右浮动直到遇到父元素或者别的浮动元素. 浮动会导致父元素高度坍塌.  解决父元素高度坍塌的方式就是清除浮动. 以下述代码为例: <styl ...

  2. 物联网学习笔记——构建RESTFul平台1

    0.前言     前些时间顺着Yeelink学习了RESTFUL,使用PHP和Slim框架尝试实现简单的REST API,树莓派可通过GET方法获得JSON数据包,通过这种方式实现了树莓派和服务器(我 ...

  3. 几款Java模板引擎的性能评测

    参评的几款模板引擎为:XMLTemplate(简称XT)Velocity(简称VT)CommonTemplate(简称CT)FreeMarker(简称FT)Smarty4j(简称ST)直接的java代 ...

  4. pythono整数和字符串魔法方法

    1.整数(int) a = 1 b = 2 c = 3 d = 4 e = 5u a1 = a.bit_length() b1 = b.bit_length() c1 = c.bit_length() ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:禁用状态

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

  6. Tensorflow 将训练模型保存为pd文件

    前言 保存 模型有2种方法. 方法 1.使用TensorFlow模型保存函数 save = tf.train.Saver() ...... saver.save(sess,"checkpoi ...

  7. f_lseek

    我在STM32中移植了fatfs文件系统,实现在SD卡对文件的读写.在普通读写中都没有问题,但是一旦我关闭文件系统,再次打开读写,之前写的数据就被覆盖.比如举个例子:       u8 tx_buff ...

  8. dmp文件自动分析

    dmp文件的分析,可以借助各种工具,比如WinDbg, CDB , NTSD,KD等.Windbg提供了窗口接口,而CDB , NTSD是基于命令行的工具,它们都使用了同样的调试引擎Dbgeng.dl ...

  9. Make the PE file consistent when code not changed

    参考:http://www.mouseos.com/assembly/06.html 参考:http://www.cnblogs.com/tk091/archive/2012/04/18/245617 ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-minus

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