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

题意:给出一个序列,求一子序列,满足其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. android开发,assets下面的资源文件不会变化/改动

    我再调试asserts下面的资源文件,发现我改动assets下面的文件内容,在真机上测试的时候还是最原先的内容,没有变,后来,卸载,重装就ok了. 原因: assets下面的资源文件,若覆盖重装,则里 ...

  2. (C#)的命名规范

    http://gray.iteye.com/blog/644626 http://developer.51cto.com/art/200908/143787.htm http://blog.csdn. ...

  3. pure.css

    注释中address是纠正的意思  等价于correct/*! Pure v0.5.0 Copyright 2014 Yahoo! Inc. All rights reserved. Licensed ...

  4. [转载]C#中int和IntPtr相互转换

    方法一. int转IntPtr int i = 12;           IntPtr p = new IntPtr(i); IntPtr转int int myi = (int)p;         ...

  5. 【WCF--初入江湖】08 并发与实例模式

    08 并发与实例模式 1. 实例上下文模式   一个服务代理:servicePoxy ChannelFactory<IService1> factoryservicel = new Cha ...

  6. spoj 78

    数学  组合 隔板法 #include <iostream> #include <cstring> #include <cstdio> #include <s ...

  7. Asp.net MVC 自定义路由在IIS7以上,提示Page Not Found 解决方法

    受限确保自定义路由在开发服务器上Ok! 然后在web.config的<webserver>节点下增加如下配置就好了.   1: <system.webServer> 2: &l ...

  8. Python性能分析指南

    http://www.admin10000.com/document/2861.html 尽管并非每个你写的Python程序都需要严格的性能分析,但了解一下Python的生态系统中很多优秀的在你需要做 ...

  9. Struts 2知识回顾----拦截器(Intercept)总结

    什么是Struts 2拦截器? 从软件构架上来说,拦截器是实现了面向方面编程的组件.它将影响了多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现功能的高度内聚,确保了业务对象的 ...

  10. nginx的健康检查功能将挂掉的Tomcat舍弃

    1.Ngninx自带健康检查功能,能将挂掉的服务器舍弃,不在访问路径里 2.Nginx还有URL重写功能,能将接收到的请求,进行改写,再将新的URL分发到后端服务器上