【CERC2013】【BZOJ4052】Magical GCD

Description

给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12。
求一个连续子序列,使得在所有的连续子序列中,它们的GCD值乘以它们的长度最大。
Input

Output

Sample Input

1

5

30 60 20 20 20
Sample Output

80
HINT

Source

以一个位置为结尾的所有连续子序列,不同Gcd的数目是log级的
所以直接暴力就是nlogn的
原因:考虑让Gcd尽可能不同的话,Gcd每次最少都要除2,所以显然是log级

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define GET (ch>='0'&&ch<='9')
#define LL long long
#define MAXN 100010
using namespace std;
int T,n,top;
struct number
{
    int id;
    LL Gcd;
}sta[MAXN];
LL ans;
LL a[MAXN];
LL gcd(LL a,LL b)   {   return !b?a:gcd(b,a%b); }
inline void in(int &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();
    while (GET) x=x*10+ch-'0',ch=getchar();
}
inline void In(LL &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();
    while (GET) x=x*10+ch-'0',ch=getchar();
}
int main()
{
    in(T);
    while (T--)
    {
        in(n);ans=0;top=0;
        for (int i=1;i<=n;i++)
        {
            In(a[i]);sta[++top]=(number){i,0};
            int Top=0;
            for (int j=1;j<=top;j++)    sta[j].Gcd=gcd(sta[j].Gcd,a[i]);
            for (int j=1;j<=top;j++)
                if (j==1||sta[j].Gcd!=sta[j-1].Gcd) sta[++Top]=sta[j];
            top=Top;
            for (int j=1;j<=top;j++)    ans=max(ans,sta[j].Gcd*(i-sta[j].id+1));
        }
        printf("%lld\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值