zoukankan      html  css  js  c++  java
  • HDU 6025 Coprime Sequence(前缀后缀GCD问题)

    Coprime Sequence

    Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 131072/131072 K(Java/Others)
    Total Submission(s): 0    Accepted Submission(s): 0

    Problem Description

    Do you know what is called``Coprime Sequence''? That is a sequence consists ofn 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 tryto 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 inputcontains an integerT(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 integersa1,a2,...,an(1≤ai≤109) , denoting the elements in the sequence.

     

     

    Output

    For each test case, print asingle 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


    题意:

    给出一系列数,它们的最大公约数为1 ,现在你需要在这些数中删掉任意一个数使剩下的书的最大公约数最大

    思路:

    依次遍历每个数,算出这个数左边的一堆数的最大公约数num1,再算出这个数右边的一堆数的最大公约数num2,gcd(num1,num2)即为删去当前数后剩下的数的最大公约数。遍历每个数得到的最大gcd即为所求。

    而每个数左边的gcd和右边的gcd可以用O(n)的时间复杂度预处理,遍历每个数是跟前面并列的O(n)复杂度,故可线性解决。


    注意:
    处理边界情况【int ans = max(num[n - 2], num2[1]);】
    枚举两边的情况【ans = max(ans, gcd(num[i - 1], num2[i + 1]));】

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    #include<bitset>
    #include<numeric>
    #include<vector>
    #include<string>
    #include<iterator>
    #include<cstring>
    #include<ctime>
    #include<functional>
    #define INF 0x3f3f3f3f
    #define ms(a,b) memset(a,b,sizeof(a))
    #define pi 3.14159265358979
    #define MOD 1000000007
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    using namespace std;
    
    typedef pair<int, int> P;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100010;
    
    int a[maxn], num[maxn], num2[maxn];
    
    int gcd(int a, int b)
    {
    	return b ? gcd(b, a%b) : a;
    }
    
    int main()
    {
    	int t;
    	scanf("%d", &t);
    	while (t--)
    	{
    		ms(num, 0);
    		ms(num2, 0);
    		int n;
    		scanf("%d", &n);
    		for (int i = 0; i < n; i++)
    		{
    			scanf("%d", a + i);
    		}
    		for (int i = 0; i < n; i++)
    		{
    			if (i == 0)
    				num[i] = a[i];
    			else
    			{
    				num[i] = gcd(a[i], num[i - 1]);
    			}
    		}
    		for (int i = n - 1; i >= 0; i--)
    		{
    			if (i == n - 1)
    				num2[i] = a[i];
    			else
    			{
    				num2[i] = gcd(num2[i + 1], a[i]);
    			}
    		}
    		int ans = max(num[n - 2], num2[1]);
    		for (int i = 1; i < n - 1; i++)
    		{
    			ans = max(ans, gcd(num[i - 1], num2[i + 1]));
    		}
    		printf("%d
    ", ans);
    	}
    }



    Fighting~
  • 相关阅读:
    java实现第二届蓝桥杯四方定理
    java实现第二届蓝桥杯四方定理
    java实现第二届蓝桥杯四方定理
    JPos学习
    Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理
    java使用Thumbnailator操作图片
    Python的另一种开发环境--Anaconda中的Spyder
    Java多线程干货系列—(四)volatile关键字
    Java多线程干货系列—(二)synchronized
    Java多线程干货系列—(一)Java多线程基础
  • 原文地址:https://www.cnblogs.com/Archger/p/12774774.html
Copyright © 2011-2022 走看看