现在的位置: 首页 > 综合 > 正文

UVA – 1642 Magical GCD(nlogn传统算法思想 序列较短的维护队列)

2019年04月03日 ⁄ 综合 ⁄ 共 975字 ⁄ 字号 评论关闭

这里维护i表示当前结尾位置只需找到快速找出最大开头j即可,考虑不同的gcd(aj,aj+1,...ai)会从后往前保持gcd值或者是变为原来的约数,

所以维护二元组(j,g) (代表gcd为g的最小位置)则序列长度最大不超过(log(maxn) = log(10^12))非常短,所以我直接采取循环刷新;每次刷新即对g = gcd(g,a[i]); 并删除g相同的元素,取最优;

#include <cstdio>
#include <vector>
#include <cmath>
#include <iostream>
#include <cstring>
using namespace std;
#define INF 200000000
typedef long long LL;
struct node{
long long p,g;
node(long long x=0,long long y=0):p(x),g(y){}
};
vector<node> pre,now;
long long  gcd(long long a,long long b){
 return b==0 ? a : gcd(b,a%b);
}
const int maxn = 100100;
long long a[maxn];
int main()
{
    int n;
    int T;
    scanf("%d",&T);
    while(T--){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
          scanf("%lld",&a[i]);
    pre.clear();
    now.clear();
    LL res=0;
    for(LL i=1;i<=n;i++){
          now.clear();
          for(LL j=0;j<pre.size();j++){
               LL te = gcd(a[i],pre[j].g);
               if(!now.empty()&&now.back().g==te)
                  continue;
               now.push_back(node(pre[j].p,te));
          }
          if(now.empty()||a[i]>now.back().g){now.push_back(node(i,a[i]));}
          for(LL j=0;j<now.size();j++){
              res=max(res,now[j].g*(i-now[j].p+1));
          }
          pre.clear();
          pre=now;
    }
    printf("%lld\n",res);
    }
    return 0;
}

抱歉!评论已关闭.