Nanyang Institute of Technology ACM Dole race-cum-retired Memorial 16 F-series operation Ⅱ

Topic Source: http://acm.nyist.edu.cn/problem/1666

Subject description:

A given sequence length N is a positive integer, and select a number to delete the remaining maximum number of the greatest common divisor.
Greatest common divisor after deletion.

Enter a description:

The first line is a number that represents N.
The second line is the number of N.
1 <= N <= 10 ^ 6, 1 <= A_i <= 10 ^ 9.

Output Description:

A number that represents the greatest common divisor of the output deleted.

Sample input:
5
21 13 9 15 12
Sample output:
3

The prefix and suffix run gcd (final comparison when selecting the first n-1 th prefix and suffix of the second largest

Reference Code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#define ll long long
const int N=1e6+5;
#define inf 0x3f3f3f3f
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
ll a[N],b[N],c[N];
int main()
{
    ll n;
    //freopen("//home/nuoyanli//文档//0615//F//data//secret//5.in", "rep", stdin);
    //freopen("output.out", "w", stdout);
    scanf("%lld",&n);
    for(int i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    if(n==1)
        printf("%lld\n",a[1]);
    else
    {
        b[1]=a[1];
        for(int i=2; i<=n; i++)
            b[i]=gcd(b[i-1],a[i]);
        c[n]=a[n];
        for(int i=n-1; i>=1; i--)
            c[i]=gcd(c[i+1],a[i]);
        ll ans=max(c[2],b[n-1]);
        for(int i=2; i<=n-1; i++)
            ans=max(ans,gcd(b[i-1],c[i+1]));
        printf("%lld\n",ans);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/nuoyanli/article/details/92447223