【NOIP2014模拟8.17】Magical GCD
题目
对于一个由正整数组成的序列, Magical GCD 是指一个区间的长度乘以该区间内所有数字的最大公约数。给你一个序列,求出这个序列最大的 Magical GCD。
分析
根据暴力的思想,
\(枚举i,枚举j,a[j]=gcd(a[j],a[i])\)
答案就是\(max(a[j]*(i-j+1))\)
显然,当\(a[j]=a[j-1]\)的时候,\(a[j]\)就一定不会更新ans,所以,弄个双向链表,把\(a[j]\)踢掉。
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const int maxlongint=2147483647;
const int mo=1000000007;
const int N=100005;
using namespace std;
long long a[N],n,m,T,p,q,last[N],next[N],ans;
long long gcd(long long x,long long y)
{
if(x!=0) return gcd(y%x,x);
else return y;
}
int main()
{
scanf("%lld",&T);
while(T--)
{
scanf("%lld",&n);
next[0]=1;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
last[i]=i-1;
next[i]=i+1;
}
last[1]=next[n]=0;
ans=0;
for(int i=1;i<=n;i++)
for(int j=next[0];j<=i && j;j=next[j])
{
a[j]=gcd(a[j],a[i]);
ans=max(a[j]*(i-j+1),ans);
if(a[j]==a[last[j]])
{
next[last[j]]=next[j];
last[next[j]]=last[j];
}
}
printf("%lld\n",ans);
}
}
【NOIP2014模拟8.17】Magical GCD的更多相关文章
- [BZOJ4052][Cerc2013]Magical GCD
[BZOJ4052][Cerc2013]Magical GCD 试题描述 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12. 求一个连续子序列,使得在所有的连续子序列中,它们 ...
- JZOJ 3927. 【NOIP2014模拟11.6】可见点数
3927. [NOIP2014模拟11.6]可见点数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description ZP ...
- 4052: [Cerc2013]Magical GCD
4052: [Cerc2013]Magical GCD Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 148 Solved: 70[Submit][ ...
- 【BZOJ】【4052】【CERC2013】Magical GCD
DP/GCD 然而蒟蒻并不会做…… Orz @lct1999神犇 首先我们肯定是要枚举下端点的……嗯就枚举右端点吧…… 那么对于不同的GCD,对应的左端点最多有log(a[i])个:因为每次gcd缩小 ...
- Magical GCD UVA 1642 利用约数个数少来优化 给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量的值最大。输出这个最大值。
/** 题目:Magical GCD UVA 1642 链接:https://vjudge.net/problem/UVA-1642 题意:给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量 ...
- 【BZOJ4052】[Cerc2013]Magical GCD 乱搞
[BZOJ4052][Cerc2013]Magical GCD Description 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12. 求一个连续子序列,使得在所有的连续 ...
- UVA - 1642 Magical GCD 数学
Magical GCD The Magical GCD of a nonempty sequence of positive integer ...
- JZOJ 3929. 【NOIP2014模拟11.6】创世纪
3929. [NOIP2014模拟11.6]创世纪 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 上帝手 ...
- JZOJ 3928. 【NOIP2014模拟11.6】射击
3928. [NOIP2014模拟11.6]射击 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 有问题, ...
随机推荐
- MariaDB 连接查询,视图,事物,索引,外键
1.连接查询 --创建学生表 create table students ( id int unsigned not null auto_increment primary key, name var ...
- ps和top进程监控
一.名词解释 1.什么是进程(what is process)? 答:进程是程序运行的过程,伴有动态,生命和运行状态.(组成:1.已分配内存的地址空间 2.安全属性,包括所有权凭据和特权 3.程序 ...
- 车牌识别1:License Plate Detection and Recognition in Unconstrained Scenarios阅读笔记
一.WHAT 论文下载地址:License Plate Detection and Recognition in Unconstrained Scenarios [pdf] github 的项目地址: ...
- 【Android Apk重新签名报错re-sign.jar之解决方法】
故障现象:
- python 有参数的装饰器
怎么样为装饰器加参数 import time current_user = {'user':None} def auth(engine = "file") def deco(fun ...
- 小程序图片预览 wx.previewImage
list: [ 'http://img5.imgtn.bdimg.com/it/u=3300305952,1328708913&fm=26&gp=0.jpg', 'http://i ...
- HDU 1042 N!(高精度阶乘、大数乘法)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submi ...
- [转帖]教你如何修改运行中的docker容器的端口映射
教你如何修改运行中的docker容器的端口映射 在docker run创建并运行容器的时候,可以通过-p指定端口映射规则.但是,我们经常会遇到刚开始忘记设置端口映射或者设置错了需要修改.当dock ...
- [转帖]vim搜索及高亮取消
vim搜索及高亮取消 https://www.cnblogs.com/soymilk2019/p/10744027.html 哎 竟然一直不知道. 1. 命令模式下,输入:/字符串 比如搜索user, ...
- C++的左值,右值,左值引用,右值引用
参考大神链接: https://blog.csdn.net/u012198575/article/details/83142419 1.左值与右值 https://msdn.microsoft.com ...