HDU6025 Coprime Sequence(gcd)
2024-09-01 16:47:55
处理出数列的 \(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;
}
最新文章
- 使用VS Code开发ASP.NET 5 应用程序
- CSS 概念 &; 作用
- VIM插件攻略
- centos7 firewalld
- Java 第四章 选择结构2
- aspose.word 在书签处插入符号
- MySQL_PHP学习笔记_2015_0923_MySQL如何开启事件
- haproxy部署及配置
- php获取https下的内容
- Word Pattern
- ACM:图BFS,迷宫
- 60、jQuery其余操作
- Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》
- Python可视化TVTK库初使用
- git如何避免push/pull时输入密码
- PHP 2 语句 数据类型 字符串函数 常量
- lame,把ios录音转换为mp3格式
- python----题库(一)
- 【week6】团队贡献分
- MongoDB初试备份及恢复
热门文章
- yii报错yii\web\Request::cookieValidationKey must be configured with a secret key.
- java线程中的同步锁和互斥锁有什么区别?
- Spring Boot嵌入式的Servlet容器
- Scala Option 从官方DOC解析
- PL/SQL中判断字段为空
- 洛谷P2401 不等数列 题解
- SpringMVC 向页面传值-Map、Model和ModelMap
- Spring、SpringMVC注解方式整合
- git学习补充
- NodeJs 提供了 exports 和 require 两个对象