题意:去除数列中的一个数字,使去除后数列中所有数字的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;
}

  

最新文章

  1. 使用js批量选中功能实现更改数据库中的status状态值(批量展示)
  2. 使用 v-cloak 防止页面加载时出现 vuejs 的变量名
  3. 用VBS实现公司自动打卡
  4. android AsyncTask实例
  5. UVa1636 Headshot
  6. 【.NET进阶】函数调用--函数栈
  7. DS实验题 Floyd最短路径 &amp; Prim最小生成树
  8. ansible自动化运维工具的安装与使用
  9. 【转】Android开发20——单个监听器监听多个按钮点击事件
  10. xml中1字节的UTF-8序列的字节1无效([字符编码]Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案)
  11. C语言函数名与函数指针详解
  12. Java中进制的转换函数
  13. [例子]Ubuntu虚拟机设置固定IP上网
  14. C++异常的几种捕获方式
  15. mybatis中批量更新的问题
  16. Mac生成ssh key
  17. SVN版本控制系统搭建(结合http服务)
  18. 2018.09.17 atcoder Digit Sum(数论)
  19. 拟牛顿法/Quasi-Newton,DFP算法/Davidon-Fletcher-Powell,及BFGS算法/Broyden-Fletcher-Goldfarb-Shanno
  20. spring mvc:练习:javaConfig配置和注解

热门文章

  1. Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机--(转)
  2. SpringCloud学习之Zuul路由转发、拦截和熔断处理(七)
  3. 如何在SecureCRT中上传文件到linux服务器上
  4. 使用 CAS 在 Tomcat 中实现单点登录 http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/
  5. 做一个php登陆页面,用pc登陆和用手机登陆弹出来的登陆页面不一样。
  6. windows编程-socket
  7. 第2节 网站点击流项目(下):6、访客visit分析
  8. vSphere中Storage vMotion的流程详解
  9. 剑指offer 把字符串转化为整数
  10. java开发之分页查询