HDU6025(rmq)
来源:互联网 发布:移动网络 编辑:程序博客网 时间:2024/05/17 00:49
Coprime Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 424 Accepted Submission(s): 230
Problem Description
Do you know what is called Coprime Sequence''? That is a sequence consists of n positive integers, and the GCD (Greatest Common Divisor) of them is equal to 1.
Coprime Sequence” is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there is an integer n(3≤n≤100000) in the first line, denoting the number of integers in the sequence.
Then the following line consists of n integers a1,a2,…,an(1≤ai≤109), denoting the elements in the sequence.
Output
For each test case, print a single line containing a single integer, denoting the maximum GCD.
Sample Input
3
3
1 1 1
5
2 2 2 3 2
4
1 2 4 8
Sample Output
1
2
2
题意:给你n个数,让你删除其中任意一个数,使剩下的数的gcd最大。
解题思路:因为只删除其中一个,所以我们很容易想到了枚举每一个要删除数字求剩下的数字的gcd就行,最后取最大值,问题就是怎样求剩下的数字的gcd,我在这里用的是rmq,然后还有一种更简单的做法,就是记录前缀gcd和后缀gcd,每次枚举时直接用就行。
#include<bits/stdc++.h>using namespace std;const int maxn = 1e5 + 10;typedef long long ll;int n;ll a[maxn];ll dp[maxn][25];int Log[maxn];ll gcd(ll x,ll y){ return x == 0?y:gcd(y%x,x);}void initRmq(){ Log[0] = -1; for(int i = 1; i <= n; i++) { Log[i] = ((i&(i - 1)) == 0)?Log[i - 1] + 1:Log[i - 1]; dp[i][0] = a[i]; } for(int j = 1; j < 25; j++) { for(int i = 1; i <= n&&(i + (1<<j) - 1) <= n; i++) { dp[i][j] = gcd(dp[i][j - 1],dp[i + (1<<(j - 1))][j - 1]); } }}ll rmq(int l,int r){ int d = r - l + 1; int j = Log[d]; return gcd(dp[l][j],dp[r - (1<<j) + 1][j]);}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1; i <= n; i++) { scanf("%lld",&a[i]); } initRmq(); ll Max = 1; ll g; for(int i = 1; i <= n; i++) { if(i == 1) g = rmq(i + 1,n); else if(i == n) g = rmq(1,n - 1); else { int g1 = rmq(1,i - 1); int g2 = rmq(i + 1,n); g = gcd(g1,g2); } Max = max(Max,g); } printf("%lld\n",Max); } return 0;}
- HDU6025(rmq)
- hdu6025(思维僵化)
- HDU6025
- HDU6025 Coprime Sequence (思路题)
- HDU6025 Coprime Sequence (前缀后缀处理)
- HDU6025-Coprime Sequence
- HDU6025 Coprime Sequence
- 女生赛hdu6025
- hdu6025 简单枚举即可
- hihoCoder - 1068 - RMQ-ST算法 (RMQ)
- ±1RMQ(约束RMQ)
- HDU6025-Coprime Sequence-简单数学
- hdu6025-构造or线段树
- RMQ (ST)
- RMQ(区间最大值)
- dp+离散(RMQ)
- 经典RMQ(poj3264)
- Balanced Lineup(RMQ)
- C++模版
- PHP计算两个日期相差天数、小时等
- C语言 MOOC 素数
- android相关链接收集
- android 灰度图 黑白图
- HDU6025(rmq)
- Linux下安装Oracle的两种方式------有图形界面安装和静默安装
- java中使用fastjson、jackson、json-lib解析JSON-------------------妈妈再也不用担心JSON解析
- eclipse--java工程转web工程 以及 java或java web工程转maven工程
- linux网络设置和虚拟机克隆转移之后Error:No suitable device found:no device found for connection 'System eth0'问题解决
- 通过Java文件来配置SpringMVC
- windows系统局域网内开启远程桌面图解
- Oracle中碰到的函数和关键字收集
- CAS单点登录(SSO)服务端的部署和配置---连接MySQL进行身份认证