首页 > 其他分享 >UVA Magical GCD

UVA Magical GCD

时间:2022-11-18 16:38:12浏览次数:62  
标签:__ maxx ma GCD int Magical UVA include first


​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;
}




标签:__,maxx,ma,GCD,int,Magical,UVA,include,first
From: https://blog.51cto.com/u_15879559/5868807

相关文章

  • UVA6588 - Crane
    ​​Regionals2013​​​>>​​​Europe-Central​​​​6588-Crane​​题意:给定一组无序数,从1-n,要求排序,排序的要求是选定一个区间[a,b],则......
  • UVA1331 题解
    前言题目传送门!更好的阅读体验?计算几何、区间DP。思路......
  • 题解 UVA12265【贩卖土地 Selling Land】
    postedon2022-09-2414:33:29|under题解|sourceproblem一个黑白矩阵,求以每个点为右下角,能围出的周长最大的全白矩形的周长。\(n\leq2000\)。solution试图进行......
  • B. Playing with GCD
    传送门题意:一个长度为n的数组a,\(a_i=gcd(b_i,b_{i+1})\),问是否存在这样的b数组能够构成a思路:总结:gcd可以推导出lcm的规律,图片中的那个>=关系是代表......
  • UVA323 Jury Compromise
    UVA323JuryCompromise-洛谷|计算机科学教育新生态(luogu.com.cn)由于选择人数有限制(等于\(m\)),因此考虑将人数设入动态规划的一维。考虑目标是\(D+P\)最大,那......
  • 裴蜀定理、exgcd与有理数取余
    裴蜀定理exgcd之前写得不好所以重写一遍exgcd即扩展欧几里得算法,常用来求\(ax+by=\gcd{(a,b)}\)的一组解。设一组解为\(x_1,y_1\),即\(ax_1+by_1=\gcd{(a,......
  • UVALive 6955 Finding Lines
    ​​点击打开链接​​随机选一条线段然后判断是否满足答案,然后执行一定的次数,基本可以保证正确。#include<cstdio>#include<ctime>#include<cstring>#include<iostream>#inc......
  • UVALive 7148 LRIP
    2014年上海区域赛的K题,树上点分治,查找差值小于等于D的非严格单调序列的最长长度。对于每个点,维护从该点出发的上升序列同长度的最小值和下降序列的同长度最大值,二分之前的得......
  • P6786 「SWTR-6」GCDs & LCMs
    题意:小A有一个长度为\(n\)的序列\(a_1,a_2,\cdots,a_n\)。他想从这些数中选出一些数\(b_1,b_2,\cdots,b_k\)满足:对于所有\(i\(1\leqi\leqk)\),\(b_i\)要么是......
  • 【模板】二元一次不定方程 exgcd
    postedon2022-09-1715:59:26|under模板|sourcecodeLLmod(LLx,LLm){return(x%m+m)%m;}LLexgcd(LLa,LLb,LLc,LL&x,LL&y){ if(!b)returnx=c/a,y=0,a;......