UVA Magical GCD

Magical GCD

题意:给定一个数列,求一个子列,该子列的最大公约数乘上子列长度的值最大,输出最大值。数列的大小是100000,这些数的大小是1-10^12。

解题思路:一开始想的是用暴力,但数据太大,优化也不行,然后想到是不是dp啊,需要的状态有点多,而且还需要更新前边的,所以也打消了,枚举吧,
枚举谁呢,最大公约数的个数也不算多,就枚举公约数吧,之后看了大神的代码,知道想法是对的,尴尬的是自己没代码实现。但看了大神的代码学到了不少东西



#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <map>

using namespace std;

const int inf = 0x3f3f3f3f;
const int Max = 440;
typedef long long LL;

LL maxx, x;
int main()
{
    int K, i, n;
    scanf("%d", &K);
    while(K--)
    {
        scanf("%d", &n);
        maxx = 0;
        map<LL, LL>ma;
        map<LL, LL>::iterator it, pos;
        for(i = 1; i <= n; i++)
        {
            cin>>x;
            for(it = ma.begin(); it != ma.end(); it++)
            {
                if(__gcd(it->first, x) != it->first)
                {
                    if(ma[__gcd(it->first, x)] == 0)
                        ma[__gcd(it->first, x)] = it->second;
                    pos = it++;
                    ma.erase(pos);
                    it--;
                }
            }
            if(ma[x] == 0)
            {
                ma[x] = i;
            }
            for(it = ma.begin(); it != ma.end(); it++)
            {
                maxx = max(maxx, (i - it->second+1)*it->first);
                //printf("%d %d\n", it->first, it->second);
            }
        }
        cout<<maxx<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值