题目大意如下:给定一个序列,每个序列有值xi,现给定t个数列,对于每个长n的数列,求一段[l,r]使 [r-l+1]*gcd(l,r)最大,gcd(l,r)指的是该连续区间的最大公约数。

不难想到n^3,n^2logx,n^2的暴力吧

n^3DP,n^2logx暴力枚举,n^2DP

可以这样考虑,每次我对于某一个数,保存若干个值,以i为右端点的区间且gcd为某一值的时候这个区间最大的左端点位置是哪里?

但是你也许会认为这样做状态会不会有点多?更新是不是n方的呢?

其实不是的,因为我们可以从左到右来递推。

什么意思呢?对于每一个数,它与前面构成的gcd一定不会太多(约数肯定不会太多),所以我们最多也只需要保存每一个约数为gcd的时候左边最远能够拓展的位置。

其实远远不要保存每一个约数的位置,因为实际上很多的约数都不是gcd,这样我们就可以由左边的所有状态和右边的一个gcd一次来递推了。

当然,我们也可以直接利用指针的自动排序特性(类似链式前向星),我们碰到一个比当前(r-l+1)*val要小的就更新,因为我们再也尝试不到比它大的了。

{$inline on}

const maxn=;

type edge=^node;
node=record
next,last:edge;
pos,val:int64;
end; var head,tail:edge;
e:array [..maxn] of edge;
n,cnt:longint;
ans:int64;
f:array [..maxn] of int64; procedure addedge(pos,value:int64); inline;
var p:edge;
begin
inc(cnt);
new(p);
p^.next:=head^.next;
p^.last:=head;
p^.next^.last:=p;
p^.last^.next:=p;
p^.pos:=pos;
p^.val:=value;
e[cnt]:=p;
end; procedure delete(var p:edge); inline;
begin
p^.last^.next:=p^.next;
p^.next^.last:=p^.last;
end; procedure init; inline;
begin
ans:=;
cnt:=;
head^.val:=; tail^.val:=;
head^.next:=tail; tail^.last:=head;
head^.last:=nil; tail^.next:=nil;
end; function gcd(a,b:int64):int64; inline;
begin
if a mod b= then exit(b)
else exit(gcd(b,a mod b));
end; function max(x,y:int64):int64; inline;
begin
if x>y then exit(x)
else exit(y);
end; procedure main;
var t,value:int64; i:longint; p:edge;
begin
read(t);
new(head); new(tail);
while t<> do
begin
dec(t);
init;
read(n);
ans:=;
for i:= to n do
begin
read(value);
addedge(i,value);
p:=head^.next;
while p<>tail do
begin
p^.val:=gcd(p^.val,value);
ans:=max(ans,p^.val*(i-p^.pos+));
if p^.val=p^.last^.val then delete(p^.last);
p:=p^.next;
end;
end;
writeln(ans);
end;
end; begin
main;
end.

CERC 2013 Magical GCD的更多相关文章

  1. 4052: [Cerc2013]Magical GCD

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

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

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

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

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

  4. 【BZOJ4052】[Cerc2013]Magical GCD 乱搞

    [BZOJ4052][Cerc2013]Magical GCD Description 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12.  求一个连续子序列,使得在所有的连续 ...

  5. [BZOJ4052][Cerc2013]Magical GCD

    [BZOJ4052][Cerc2013]Magical GCD 试题描述 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12.  求一个连续子序列,使得在所有的连续子序列中,它们 ...

  6. UVA - 1642 Magical GCD 数学

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

  7. 【NOIP2014模拟8.17】Magical GCD

    题目 对于一个由正整数组成的序列, Magical GCD 是指一个区间的长度乘以该区间内所有数字的最大公约数.给你一个序列,求出这个序列最大的 Magical GCD. 分析 根据暴力的思想, \( ...

  8. BZOJ 4052: [Cerc2013]Magical GCD

    以一个数字开头的子序列的gcd种类不会超过logn种,因此去找相同gcd最长的位置,更新一下答案,复杂度O(nlogn^2) #include<cstdio> #include<al ...

  9. uva 1642 Magical GCD

    很经典的题目,愣是没做出来.. 题意:给出一个序列,求一子序列,满足其GCD(子序列)* length(子序列)最大. 题解: 类似单调队列的思想,每次将前面所得的最大公约数与当前数进行GCD,若GC ...

随机推荐

  1. 选项卡 js操作

    html代码展示(这里展示的是关于日程的标签页)css样式这里省略了>>>>自己写的可能更好看 <div class="row"> <ul ...

  2. tomcat部署java项目

    tomcat部署java项目 1.启动tomcat 进入到tomcat安装目录下的bin #cd /opt/tomcat/bin #./startup.sh // 执行重启命令 2.重建一个新目录导入 ...

  3. JAVA之GUI编程窗体事件

    package GUI; import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt ...

  4. codeforces 633G. Yash And Trees dfs序+线段树+bitset

    题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...

  5. ffmpeg用法

    1. help ffmpeg.exe -h > help.txt 2. 解码: ffmpeg -i 123.264 123.yuv ffmpeg -i 123.264 -vframes 200  ...

  6. linux 使用者管理

    1.用户标识符 UID  用户ID GID  用户组ID 2./etc/passwd 文件结构 id范围:0系统管理员|1~499 (系统账号)|500~65535 可登录账号

  7. was配置oracle RAC集群的数据源

    在WebSphere中配置配置Oracle RAC集群的数据源,假设Oracle RAC双击分别为 HOST1 与 HOST2 , 端口为1521 ,服务名为 orcldbservice,则配置的UR ...

  8. HDU1200:To and Fro

    Problem Description Mo and Larry have devised a way of encrypting messages. They first decide secret ...

  9. jquery 插件 validate 学习

    jquery是十分方便的对于现在来说. 首先应该明白一个问题: <p> <label  for="password">Password</label& ...

  10. SQL Server索引进阶:第十级,索引内部结构

    原文地址: Stairway to SQL Server Indexes: Level 10,Index Internal Structure 本文是SQL Server索引进阶系列(Stairway ...