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 数学的更多相关文章

  1. UVa 1642 - Magical GCD(数论)

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

  2. uva 1642 Magical GCD

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

  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. C# 添加应用程序包

    项目中可能会遇到某些扩展方法不可以应用的情况,这时候需要在项目中引用程序包 步骤如下: 回车即可

  2. win10下mysql安装过程中遇到的各种坑

    前几天重装系统,又要下回来mysql,但没想到还是遇到了许多麻烦,翻了十多篇博文才搞定,写个总结出来方便以后不要重复踩坑,也给大家参考参考. 1.下载与安装 这个没什么好说的,下载地址网上一大堆,安装 ...

  3. 关于VS 2013连接Microsoft Access 2013的相关问题

      ①   下载安装Microsoft Access Database Engine 2010 Redistributable(28MB),共有32bit(下载)和64bit(下载)两个版本,具体要安 ...

  4. easyui的datetimebox时间格式化详解

    今天公司让用easyui的datetimebox组件,而且还要让格式化成大家通用的那种,网上搜了很多,但差不多都是复制黏贴的,最后请教了下螃蟹. 感谢螃蟹抽空给做了个例子,现在拿出来和大家分享下,效果 ...

  5. UVa1585修改版

    #include<stdio.h> int main() { int i,k=-1; char a[100]; while(scanf("%s",&a)!=EO ...

  6. type="radio"样式修改

    input[type=radio],input[type=checkbox] { display: inline-block; vertical-align: middle; width: 20px; ...

  7. 如何检查Windows网络通信端口占用

    最近本地测试jsp程序发现tomcat启动失败,无法监听8080端口,也没记得别的什么程序占用了8080端口,干脆就改成了8090端口先用着.今天找了找Windows上查看网络通信端口占用的方法. 先 ...

  8. PHP如何判断一个数组是一维的还是二维的

    1.首先我们有一个PHP内置函数 count(); count(array,mode); array是数组,mode默认为0,1是递归的计数 <?php if(count($arr) == co ...

  9. Project Euler 47 Distinct primes factors( 筛法记录不同素因子个数 )

    题意: 首次出现连续两个数均有两个不同的质因数是在: 14 = 2 × 715 = 3 × 5 首次出现连续三个数均有三个不同的质因数是在: 644 = 22 × 7 × 23645 = 3 × 5 ...

  10. Project Euler 2 Even Fibonacci numbers

    题意:斐波那契数列中的每一项都是前两项的和.由1和2开始生成的斐波那契数列前10项为:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, -考虑该斐波那契数列中不超过四百万的项,求其 ...