HDU6025 Coprime Sequence

处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) 。

遍历数列取 \(max\) 即为答案。

时间复杂度为 \(O(n)\) 。

#include<bits/stdc++.h>

using namespace std;

const int maxn = 100005;
int a[maxn], head[maxn], tail[maxn];
int t, n; int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
for(scanf("%d", &t); t--; ){
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
head[0] = tail[n + 1] = 0;
for(int i = 1; i <= n; i++) head[i] = gcd(head[i - 1], a[i]);
for(int i = n; i >= 1; i--) tail[i] = gcd(tail[i + 1], a[i]);
int ans = 0;
for(int i = 1; i <= n; i++){
ans = max(ans, gcd(head[i - 1], tail[i + 1]));
}
cout << ans << endl;
}
return 0;
}

最新文章

  1. 使用VS Code开发ASP.NET 5 应用程序
  2. CSS 概念 &amp; 作用
  3. VIM插件攻略
  4. centos7 firewalld
  5. Java 第四章 选择结构2
  6. aspose.word 在书签处插入符号
  7. MySQL_PHP学习笔记_2015_0923_MySQL如何开启事件
  8. haproxy部署及配置
  9. php获取https下的内容
  10. Word Pattern
  11. ACM:图BFS,迷宫
  12. 60、jQuery其余操作
  13. Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》
  14. Python可视化TVTK库初使用
  15. git如何避免push/pull时输入密码
  16. PHP 2 语句 数据类型 字符串函数 常量
  17. lame,把ios录音转换为mp3格式
  18. python----题库(一)
  19. 【week6】团队贡献分
  20. MongoDB初试备份及恢复

热门文章

  1. yii报错yii\web\Request::cookieValidationKey must be configured with a secret key.
  2. java线程中的同步锁和互斥锁有什么区别?
  3. Spring Boot嵌入式的Servlet容器
  4. Scala Option 从官方DOC解析
  5. PL/SQL中判断字段为空
  6. 洛谷P2401 不等数列 题解
  7. SpringMVC 向页面传值-Map、Model和ModelMap
  8. Spring、SpringMVC注解方式整合
  9. git学习补充
  10. NodeJs 提供了 exports 和 require 两个对象