• BNU Online Judge-34777-Magical GCD


    题目链接

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=34777

    题意 

    如样例

    输入

    1
    5
    30 60 20 20 20

    输出

    80

    如  30 和 60 的最大公约数是 30 他们是两个数 因而为2*30=  60  但他不是最大的  60 20 20 20 的最大公约数是 20  他们有四个数 4*20=80;他是最大的

    所以输出 80;

    代码     1;

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <set>
    #define ll long long
    using namespace std;
    ll a[100000+10];
    map<ll,ll>v;
    map<ll,ll>::iterator it,itit;
    ll Max(ll x,ll y)
    {
    return x>y?x:y;
    }
    ll gcd(ll a,ll b)
    {
    if(a==0) return b;
    if(b==0) return a;
    return gcd(b,a%b);
    }
    int main()
    {
    int t,n;
    scanf("%d",&t);
    while (t--)
    {
    ll ans=0;
    scanf("%d",&n);
    v.clear();
    for (int i=1;i<=n;i++)
    {
    scanf("%lld",&a[i]);
    ans=Max(ans,a[i]);
    for (it=v.begin();it!=v.end();it++)
    {
    if(gcd(it->first,a[i])!=it->first)
    {
    if (v[gcd(it->first,a[i])]==0)
    v[gcd(it->first,a[i])]=it->second;
    itit=it++;
    v.erase(itit,it);
    it--;
    }
    }
    if (v[a[i]]==0)v[a[i]]=i;
    for (it=v.begin();it!=v.end();it++)
    {
    ans=Max(ans,(it->first)*(i-it->second+1));
    }
    }
    printf("%lld ",ans);
    }
    return 0;
    }

    代码   2;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define LL long long
    LL num[100010];
    struct node
    {
    LL g,len;
    int t;
    } gc[100010];
    LL gcd(LL a,LL b)
    {
    if(!b) return a;
    else return gcd(b,a%b);
    }
    LL ma(LL a,LL b)

    {
    return a>b?a:b;
    }
    int main()
    {
    int t;
    scanf("%d",&t);
    while(t--)
    {
    int n,len=0,st=-1;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    scanf("%lld",&num[i]);
    LL maxx=0;
    for(int i=0; i<n; i++)
    {
    maxx=ma(maxx,num[i]);
    gc[len].g=num[i];
    gc[len].len=1;
    gc[len].t=st;
    st=len++;
    int la=st;
    for(int j=gc[st].t; j>=0; j=gc[j].t)
    {
    gc[j].g=gcd(gc[j].g,num[i]);
    while(gc[j].t>=0&&gc[j].g==gcd(num[i],gc[gc[j].t].g))
    {
    //printf("%d %d: %lld %lld ",j,gc[j].t,gc[j].g,gcd(num[i],gc[gc[j].t].g));
    gc[gc[j].t].g=gc[j].g;
    j=gc[j].t;
    }
    gc[la].t=j;
    gc[j].len++;
    la=j;
    maxx=ma(maxx,gc[j].len*gc[j].g);
    }
    //for(int j=st; j>=0; j=gc[j].t)
    // printf("%lld %d ",gc[j].g,gc[j].len);
    //puts("");
    }
    printf("%lld ",maxx);
    }
    return 0;
    }

  • 相关阅读:
    css3学习笔记之效果
    css3学习笔记之2D转换
    css3学习笔记之文本效果
    css3学习笔记之渐变
    css3学习笔记之背景
    css3学习笔记之边框
    AngularJs 入门参考代码
    环形矩阵
    Qt编写串口通信程序全程图文讲解[转]
    Qt操作Sqlite数据库
  • 原文地址:https://www.cnblogs.com/liudehao/p/4008646.html
Copyright © 2020-2023  润新知