很经典的题目,愣是没做出来。。

题意:给出一个序列,求一子序列,满足其GCD(子序列)* length(子序列)最大。

题解:

  类似单调队列的思想,每次将前面所得的最大公约数与当前数进行GCD,若GCD变小,则将原来的最大公约数替换成当前GCD,因为原来的已经不可能取到了。

  实现时利用的是STL中的map。另外经WLM牛的指点,得知map在遍历时插入删除是十分危险的操作,最后用滚动map实现。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define LL long long using namespace std; LL ans, a;
int n, t;
map<LL, LL> mapp[]; LL gcd(LL a, LL b)
{
return b?gcd(b, a%b):a;
} int main()
{
scanf("%d", &t);
while(t--)
{
ans=;
scanf("%d", &n);
int k=;
mapp[k].clear();
for(int i=; i<=n; i++, k^=)
{
scanf("%I64d", &a);
mapp[!k].clear();
ans=max(ans, a);
if(mapp[!k].find(a)==mapp[!k].end())
mapp[!k][a]=i;
for(map<LL, LL>::iterator it=mapp[k].begin(); it != mapp[k].end(); it++)
{
LL tmp=gcd(a, it->first);
ans=max(ans, tmp*(i-it->second+));
if(mapp[!k].find(tmp)==mapp[!k].end())
mapp[!k][tmp]=it->second;
else if(mapp[!k][tmp]>it->second)
mapp[!k][tmp]=it->second;
}
}
printf("%I64d\n", ans);
}
return ;
}

当然不用滚动也可以,因为加入的元素都小于当前元素,不会影响到后面,其次删除时 用 map.erase(it++)就可以了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define LL long long using namespace std; LL ans, a;
int n, t;
map<LL, LL> mapp; LL gcd(LL a, LL b)
{
return b?gcd(b, a%b):a;
} int main()
{
scanf("%d", &t);
while(t--)
{
ans=;
scanf("%d", &n);
mapp.clear();
for(int i=; i<=n; i++)
{
scanf("%I64d", &a);
if(mapp.find(a) == mapp.end())
mapp[a]=i;
for(map<LL, LL>::iterator it=mapp.begin(); it != mapp.end(); )
{
LL tmp=gcd(a, it->first);
ans=max(ans, tmp*(i-it->second+));
if(mapp.find(tmp) == mapp.end())
mapp[tmp]=it->second;
else
mapp[tmp]=min(mapp[tmp], it->second);
if(tmp<it->first)
mapp.erase(it++);
else
it++;
}
}
printf("%I64d\n", ans);
}
return ;
}

uva 1642 Magical GCD的更多相关文章

  1. UVA - 1642 Magical GCD 数学

                                  Magical GCD The Magical GCD of a nonempty sequence of positive integer ...

  2. UVa 1642 - Magical GCD(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 1642 Magical GCD(经典gcd)

    题意:给你n(n<=100000)个正整数,求一个连续子序列使序列的所有元素的最大公约数与个数乘积最大 题解:我们知道一个原理就是对于n+1个数与n个数的最大公约数要么相等,要么减小并且减小至少 ...

  4. UVA 1642 Magical GCD(gcd的性质,递推)

    分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...

  5. UVa 1642 Magical GCD (暴力+数论)

    题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...

  6. Magical GCD UVA 1642 利用约数个数少来优化 给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量的值最大。输出这个最大值。

    /** 题目:Magical GCD UVA 1642 链接:https://vjudge.net/problem/UVA-1642 题意:给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量 ...

  7. uva 10951 - Polynomial GCD(欧几里得)

    题目链接:uva 10951 - Polynomial GCD 题目大意:给出n和两个多项式,求两个多项式在全部操作均模n的情况下最大公约数是多少. 解题思路:欧几里得算法,就是为多项式这个数据类型重 ...

  8. 4052: [Cerc2013]Magical GCD

    4052: [Cerc2013]Magical GCD Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 148  Solved: 70[Submit][ ...

  9. 【BZOJ】【4052】【CERC2013】Magical GCD

    DP/GCD 然而蒟蒻并不会做…… Orz @lct1999神犇 首先我们肯定是要枚举下端点的……嗯就枚举右端点吧…… 那么对于不同的GCD,对应的左端点最多有log(a[i])个:因为每次gcd缩小 ...

随机推荐

  1. 阿里云服务器上安装mysql的心路历程(博友们进来看看哦)

    在阿里云花了100买了一台云服务器,配置如下: CPU: 1核 内存: 512MB 数据盘: 0G 带宽: 1Mbps 阿里云服务器安装mysql搞得我想吐血,搞了一个多星期,现在才搞好,而且,还有许 ...

  2. Oracle 新建序列值

    create sequence MSG_OUTBOX_ID_SEQ minvalue maxvalue start increment cache ;

  3. PHP Cookie处理函数

    (o゜▽゜)o☆[BINGO!] ok,我们先看看cookie是什么东东? cookie是服务器留在客户端的用于识别用户或者存储一些数据的小文件(注意,session存储在服务器端,这是两者的区别之一 ...

  4. 推荐acm题目

    杭电  http://acm.hdu.edu.cn/onlineuser.php. 浙大  http://acm.zju.edu.cn/onlinejudge/submit.do?problemId= ...

  5. JSON-JObject

    http://james.newtonking.com/json/help/index.html http://www.cnblogs.com/usharei/archive/2012/04/24/2 ...

  6. 【POJ】【3537】Crosses and Crosses

    博弈论 相当于放了x的位置,左右4格都不能再放x了,谁无处可放就输. n<=2000 直接枚举后继状态,暴力求SG函数即可. 例: 0000000->x..0000 / .x..000 / ...

  7. Unity3D脚本中文系列教程(十)

    http://dong2008hong.blog.163.com/blog/static/4696882720140312627682/?suggestedreading&wumii Unit ...

  8. Indent Guides VS 插件 对齐线

  9. POJ1004Financial Management

    这个题犯了一个小小的错误,double输出的时候用f才对,输入用lf即可.... http://poj.org/problem?id=1004 #include<stdio.h> int ...

  10. 使用wget和ftp共享文件

    一.需求 有一个机器A,上面那有很多文件.现在新买一个机器B,不想用U盘复制,就想把A弄成个服务器,然后B登录到A,想要什么文件就下载什么文件. 二.Win7实现 A是Win7和Ubuntu双系统,首 ...