UVA 1642 Magical GCD (思路+哈希map)

大体题意:

给你n 个整数(n <= 1e5)要求选择一个连续的子序列,使得gcd * 长度 最大?

思路:

按照刘汝佳分析做的,加入一个数后,给它的所有后缀的gcd 打一个表,gcd 相同的 保留长度大的,其余的全部删除。

那么这个表中元素数量最大是log2 j 个,很小的一个数。

因此随便做了,但是不能用有序的stl  如set 还是会超时。

用两个Hash map 来会导就可以了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>
#include <unordered_map>
#define Max(a,b) ((a)>(b)?(a):(b))
#define ps push_back
#define fi first
#define se second
#define Siz(x) (int)x.size()


using namespace std;

typedef long long LL;

const int maxn = 1e5 + 10;
LL gcd(LL a,LL b){
    return !b ? a : gcd(b,a%b);
}
LL a[maxn];
LL b[maxn];
unordered_map <LL,int>mp,mp2;
unordered_map<LL,int>::iterator it;

int T, n;
int main(){


    scanf("%d",&T);
    while(T--){
        LL ans = 0;
        mp.clear();
        scanf("%d",&n);
        for (int i = 1; i <= n; ++i){
            scanf("%lld",a+i);
            ans = Max(ans,a[i]);
        }

        mp[a[1] ] = 1;
        for (int i = 2; i <= n; ++i){
            int cnt = 0;
            mp2.clear();
            for (it = mp.begin(); it != mp.end();++it){
                LL ng = gcd(it->fi, a[i]);
                LL nlen = it->se + 1;
                if (!mp2.count(ng)){
                    mp2[ng] = nlen;
                    ans = Max(ans,nlen*ng);
                }
                else {
                    if (mp2[ng] < nlen){
                        mp2[ng] = nlen;
                        ans = Max(ans,nlen*ng);
                    }
                    else ans = Max(ans,mp2[ng]*ng);
                }
            }
            if (!mp2.count(a[i]))mp2[a[i] ] = 1;
            mp = mp2;
        }
        printf("%lld\n",ans);
    }

    return 0;
}
/**

2
5
30 60 20 20 20

4
2 2 3 4

ans = 80
**/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值