UVA - 1642 Magical GCD 数学
Magical GCD
The Magical GCD of a nonempty sequence of positive integers is defined as the product of its length
and the greatest common divisor of all its elements.
Given a sequence (a1, . . . , an), find the largest possible Magical GCD of its connected subsequence.
Input
The first line of input contains the number of test cases T. The descriptions of the test cases follow:
The description of each test case starts with a line containing a single integer n, 1 ≤ n ≤ 100000.
The next line contains the sequence a1, a2, . . . , an, 1 ≤ ai ≤ 1012
.
Output
For each test case output one line containing a single integer: the largest Magical GCD of a connected
subsequence of the input sequence.
Sample Input
1
5
30 60 20 20 20
Sample Output
80
题意:
给你N个数,求一个连续子序列,使得该序列中所有的最大公约数与序列长度的乘积最大
题解:
首先明确的做法是:枚举右端点,然后找到一个答案最大的左端点更新答案
那么如何找到这个最大的左端点,
假设我们求出了前i个数每个j(1<=j<=i) 的匹配的最优左端点,且gcd值,对应pos位置值已知,
那么我们可以根据gcd在非递增下,去更新这些gcd值和gcd值相同情况下 最左的左端点
这样的复杂度是nlogn的,
不同gcd至少相差2倍,我们就可以知道它是log的了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5+, M = , mod = 1e9+, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0 int T,n;
ll a[N];
ll gcd(ll a, ll b) { return b == ? a : gcd(b, a%b); }
vector<pair<ll,int > > v;
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
v.clear();
ll ans = ;
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
for(int j=;j<=n;j++) {
v.push_back(make_pair(,j));
int k = v.size();
for(int i=;i<k;i++) {
v[i].first = (gcd(v[i].first,a[j]));
}
sort(v.begin(),v.end());
vector<pair<ll,int > > now;
for(int i=;i<v.size();i++) {
if(i == || v[i-].first != v[i].first) {
now.push_back(v[i]);
ans = max(ans, 1ll*v[i].first*(j - v[i].second+));
}
}
v = now;
}
cout<<ans<<endl;
}
return ;
}
UVA - 1642 Magical GCD 数学的更多相关文章
- UVa 1642 - Magical GCD(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 1642 Magical GCD
很经典的题目,愣是没做出来.. 题意:给出一个序列,求一子序列,满足其GCD(子序列)* length(子序列)最大. 题解: 类似单调队列的思想,每次将前面所得的最大公约数与当前数进行GCD,若GC ...
- UVA 1642 Magical GCD(经典gcd)
题意:给你n(n<=100000)个正整数,求一个连续子序列使序列的所有元素的最大公约数与个数乘积最大 题解:我们知道一个原理就是对于n+1个数与n个数的最大公约数要么相等,要么减小并且减小至少 ...
- 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 ...
- UVa 1642 Magical GCD (暴力+数论)
题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...
- Magical GCD UVA 1642 利用约数个数少来优化 给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量的值最大。输出这个最大值。
/** 题目:Magical GCD UVA 1642 链接:https://vjudge.net/problem/UVA-1642 题意:给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量 ...
- uva 10951 - Polynomial GCD(欧几里得)
题目链接:uva 10951 - Polynomial GCD 题目大意:给出n和两个多项式,求两个多项式在全部操作均模n的情况下最大公约数是多少. 解题思路:欧几里得算法,就是为多项式这个数据类型重 ...
- 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缩小 ...
随机推荐
- FastDFS介绍(非原创)
文章大纲 一.FastDFS介绍二.FastDFS安装与启动(Linux系统)三.Java客户端上传图片四.参考文章 一.FastDFS介绍 1. 什么是FastDFS FastDFS是用C语言编 ...
- Spring《八》AOP/代理类定义
Spring通知 Interception Around通知 MethodInterceptor类(方法执行前后通知) Before通知 MethodBeforeAdvice类(方法执行前通知) Af ...
- JavaScrip——插入地图
具体操作步骤:1.百度搜索:百度地图生成器 2.打开第一个,复制网址http://api.map.baidu.com/lbsapi/creatmap/index.html,打开3.页面显示为 4.根据 ...
- 第4章 部署模式 Three-Tiered Distribution(三级分布)
影响因素 Tiered Distribution 中讨论的影响因素也适用于此模式.有关这些通用影响因素的讨论,请参阅"Tiered Distribution".下列影响因素仅适用于 ...
- YCbCr to RGB and RGB toYCbCr
RGB => YCbCr: Y = 0.299R + 0.587G + 0.114BCb = -0.1726R - 0.3388G + 0.5114B + 128Cr = 0.5114R - 0 ...
- Intel VTune Amplifier XE 使用
VTune <VTune 开发者手册> 1. 安装 1.1 软件安装 下载: (安装包下载地址) 安装: # 1.解压 tar -zxvf filename.tar.gz # 2.安装 c ...
- 使用eclipse,对spring boot 进行springloader或者devtool热部署失败处理方法
确定配置进行依赖和配置没有错误后. 调整spring boot 的版本,因为新版本对老版本的spring boot 不能使用. 改为: <groupId>org.springframewo ...
- SweetAlert详解
官方给出的SweetAlert介绍是:SweetAlert可以替代JavaScript原生的alert和confirm等函数呈现的弹出提示框,它将提示框进行了美化,并且允许自定义,支持设置提示框标题. ...
- 关于MySQL日期操作函数 date_formate 的使用
基本语法:DATE_FORMAT(date,format)说明:date 参数是合法的日期.format 规定日期/时间的输出格式.可以用的格式主要有格式 描述%a 缩写星期名%b 缩写月名%c 月, ...
- js 背景从无到黑的渐变 字从白到黑的渐变
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...