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

Do you know what is called ``Coprime Sequence‘‘? That is a sequence consists of nnpositive 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.

InputThe first line of the input contains an integer T(1≤T≤10)T(1≤T≤10), denoting the number of test cases. 
In each test case, there is an integer n(3≤n≤100000)n(3≤n≤100000) in the first line, denoting the number of integers in the sequence. 
Then the following line consists of nn integers a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the elements in the sequence.OutputFor 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

去掉一个值,使得剩下的值gcd最大。典型的预处理。将每个值的前缀和后缀求出来,扫一遍每个值,ans=max(ans,gcd(前缀,后缀))

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<set>
#include<map>
#include<algorithm>
#define MAX 100005
#define INF 0x3f3f3f3f
using namespace std;

int a[MAX];
int pre[MAX],sub[MAX];
int gcd(int x,int y){
    if(y) return gcd(y,x%y);
    return x;
}
int main()
{
    int t,f,h,n,m,k,i,j;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        scanf("%d",&a[1]);
        pre[1]=a[1];
        for(i=2;i<=n;i++){
            scanf("%d",&a[i]);
            pre[i]=gcd(pre[i-1],a[i]);
        }
        sub[n]=a[n];
        for(i=n-1;i>=1;i--){
            sub[i]=gcd(sub[i+1],a[i]);
        }
        int maxx=(pre[n-1]>sub[2]?pre[n-1]:sub[2]);
        for(i=2;i<n;i++){
            if(gcd(pre[i-1],sub[i+1])>maxx) maxx=gcd(pre[i-1],sub[i+1]);
        }
        printf("%d\n",maxx);
    }
    return 0;
}  
 

原文地址:https://www.cnblogs.com/yzm10/p/9531953.html

时间: 2024-10-10 02:50:04

HDU - 6025 Coprime Sequence(gcd+前缀后缀)的相关文章

hdu 6025 Coprime Sequence (前后缀GCD)

题目链接:hdu 6025 Coprime Sequence 题意: 给你n个数,让你删掉一个数,使得剩下的数的gcd最大 题解: 先将这一列数的前缀后缀gcd预处理一下. 然后挨着for一下就行了 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 const int N=1e5+7; 6 int t,pre[N],suf[N],n,a[N]; 7 8

HDU 6025 Coprime Sequence

Coprime Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 44    Accepted Submission(s): 34 Problem Description Do you know what is called ``Coprime Sequence''? That is a sequence consis

hdu 4691 最长公共前缀 后缀数组 +lcp+rmq

http://acm.hdu.edu.cn/showproblem.php?pid=4691 去年暑假多校赛的题,当时还不会后缀数组 现在会了,其实自己组合后缀数组跟rmq还是对的,但是题意理解有问题,于是折腾了很久,,,, 此处简单解释下题目样例吧,希望对读者有帮助  以最后一组数据为例 myxophytamyxopodnabnabbednabbingnabit 6 0 9 9 16 16 19 19 25 25 32 32 37 前两行不解释,题目叙述很清楚 从第三行,0 9 指的是第一个字

HDU 6186 CS Course(前缀+后缀)

http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. 思路:分别计算出&.|.^的前缀和后缀,将前缀和后缀相计算即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 const int ma

hdu 6025 前缀 后缀 gcd

大致题意: 去掉一个元素能使这个数列的GCD最大为多少 分析: 我们求一个数列的GCD,是先求前两个元素的GCD,然后将这个GCD值在与下一个元素进行GCD运算.由此可知进行GCD运算的顺序对最终的结果是没有影响的.我们再看看数列的长度范围,小于100000.那我们就枚举去掉的那个元素,那么去掉元素后的数列的GCD即这个元素前面一段数列的GCD再与这个元素后面一段数列的GCD进行GCD运算.所以我们需要两个数组分别记录前缀GCD和后缀GCD,这两个数组都可以通过O(n)算法算出来. #inclu

2017 ccpc女生专场 1003 Coprime Sequence

前缀后缀gcd,其实自己中用的是种奇怪的方法A掉的,不过先把这个学上,自己的方法有时间再填. 题意 告诉你N个数,求删除一个数可以求得最大GCD. N可能是100000. 思路 这道题其实很简单,但是想不到这点就很难. 简单的说就是先预处理,得到每个数字左边的GCD和右边的GCD. befor(i)代表前i个数字的GCD, 复杂度 O(n*log(n)) after(i)代表i之后的数字的GCD. 复杂度 O(n*log(n)) ans = max(after(2), befor(1)+afte

HDU 4910 Problem about GCD

Problem about GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 470    Accepted Submission(s): 77 Problem Description Given integer m. Find multiplication of all 1<=a<=m such gcd(a, m)=1 (cop

HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小到大排序后将 排序后的后缀的开头位置 顺次放入sa中,则sa[i]储存的是排第i大的后缀的开头位置.简单的记忆就是“排第几的是谁”. //名次数组rank:rank[i]保存的是suffix(i){后缀}在所有后缀中从小到大排列的名次.则 若 sa[i]=j,则 rank[j]=i.简单的记忆就是“你排第几”

hdu 6025

题意:t组,n个数,去掉任意一个数,使n-1个数的GCD最大 思路:求前缀gcd,后缀gcd,然后max(gcd(gcd(1,i),gcd(i+1,n)). 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int a[100004]; 5 int b[100004]; 6 int c[100004]; 7 8 int main(){ 9 int t; 10 scanf("%d&quo