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~
  • 相关阅读:
    价值理论的出发点和落脚点都是人--以人为本
    价值理论是人类决策和行为的标尺
    事实判断和价值判断
    什么是价值理论?---人们认识世界和改造世界的过程可分解为四个基本阶段
    大人只看利弊 小孩才分对错
    为人处世、事实判断和价值判断皆不可少--人类认识客观事物的标尺:对错与利弊
    知行之间--价值与真理--行动的标尺
    事实判断与价值判断之间的桥梁就是人的需要
    10分钟梳理MySQL核心知识点
    postman设置环境变量,实现一套接口根据选择的环境去请求不同的url
  • 原文地址:https://www.cnblogs.com/Archger/p/8451650.html
Copyright © 2011-2022 走看看