BNU Online Judge-34777-Magical GCD
题目链接
http://www.bnuoj.com/bnuoj/problem_show.php?pid=34777
题意
如样例
输入
1
5
30 60 20 20 20
输出
80
如 30 和 60 的最大公约数是 30 他们是两个数 因而为2*30= 60 但他不是最大的 60 20 20 20 的最大公约数是 20 他们有四个数 4*20=80;他是最大的
所以输出 80;
代码 1;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#define ll long long
using namespace std;
ll a[100000+10];
map<ll,ll>v;
map<ll,ll>::iterator it,itit;
ll Max(ll x,ll y)
{
return x>y?x:y;
}
ll gcd(ll a,ll b)
{
if(a==0) return b;
if(b==0) return a;
return gcd(b,a%b);
}
int main()
{
int t,n;
scanf("%d",&t);
while (t--)
{
ll ans=0;
scanf("%d",&n);
v.clear();
for (int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
ans=Max(ans,a[i]);
for (it=v.begin();it!=v.end();it++)
{
if(gcd(it->first,a[i])!=it->first)
{
if (v[gcd(it->first,a[i])]==0)
v[gcd(it->first,a[i])]=it->second;
itit=it++;
v.erase(itit,it);
it--;
}
}
if (v[a[i]]==0)v[a[i]]=i;
for (it=v.begin();it!=v.end();it++)
{
ans=Max(ans,(it->first)*(i-it->second+1));
}
}
printf("%lld\n",ans);
}
return 0;
}
代码 2;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
LL num[100010];
struct node
{
LL g,len;
int t;
} gc[100010];
LL gcd(LL a,LL b)
{
if(!b) return a;
else return gcd(b,a%b);
}
LL ma(LL a,LL b)
{
return a>b?a:b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,len=0,st=-1;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%lld",&num[i]);
LL maxx=0;
for(int i=0; i<n; i++)
{
maxx=ma(maxx,num[i]);
gc[len].g=num[i];
gc[len].len=1;
gc[len].t=st;
st=len++;
int la=st;
for(int j=gc[st].t; j>=0; j=gc[j].t)
{
gc[j].g=gcd(gc[j].g,num[i]);
while(gc[j].t>=0&&gc[j].g==gcd(num[i],gc[gc[j].t].g))
{
//printf("%d %d: %lld %lld\n",j,gc[j].t,gc[j].g,gcd(num[i],gc[gc[j].t].g));
gc[gc[j].t].g=gc[j].g;
j=gc[j].t;
}
gc[la].t=j;
gc[j].len++;
la=j;
maxx=ma(maxx,gc[j].len*gc[j].g);
}
//for(int j=st; j>=0; j=gc[j].t)
// printf("%lld %d\n",gc[j].g,gc[j].len);
//puts("");
}
printf("%lld\n",maxx);
}
return 0;
}
BNU Online Judge-34777-Magical GCD的更多相关文章
- 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. 求一个连续子序列,使得在所有的连续 ...
- [BZOJ4052][Cerc2013]Magical GCD
[BZOJ4052][Cerc2013]Magical GCD 试题描述 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12. 求一个连续子序列,使得在所有的连续子序列中,它们 ...
- UVA - 1642 Magical GCD 数学
Magical GCD The Magical GCD of a nonempty sequence of positive integer ...
- 【NOIP2014模拟8.17】Magical GCD
题目 对于一个由正整数组成的序列, Magical GCD 是指一个区间的长度乘以该区间内所有数字的最大公约数.给你一个序列,求出这个序列最大的 Magical GCD. 分析 根据暴力的思想, \( ...
- Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls 总时间限制: 1000ms 内存限制: 262144kB 描述 Wenwen has a magical ball. When put on an infin ...
- BZOJ 4052: [Cerc2013]Magical GCD
以一个数字开头的子序列的gcd种类不会超过logn种,因此去找相同gcd最长的位置,更新一下答案,复杂度O(nlogn^2) #include<cstdio> #include<al ...
- uva 1642 Magical GCD
很经典的题目,愣是没做出来.. 题意:给出一个序列,求一子序列,满足其GCD(子序列)* length(子序列)最大. 题解: 类似单调队列的思想,每次将前面所得的最大公约数与当前数进行GCD,若GC ...
随机推荐
- Android--->activity高级运用,保存前一个界面为完成的数据savedInstanceState。
main.xml布局代码分析 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- Android之EditText控件
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- Python异常处理体系
1.Python内建异常体系结构 The class hierarchy for built-in exceptions is: BaseException +-- SystemExit +-- ...
- Mysql 技巧
order by条件: SELECT * FROM tablename WHERE id_one=27 OR id_two=27 ORDER BY CASE WHEN id_one=27 THEN t ...
- The 2014 ACM-ICPC Asia Regional Anshan Online
[A]无向图的双联通子图计数.DP+状态压缩 [B]计算几何(点的旋转) [C]DP+状态压缩 [D]离散数学+DP (感觉可出) [E]概率DP [F]LCT模板题(-_-///LCT是啥!!!!) ...
- 函数之return
return语句用来从一个函数 返回 即跳出函数.我们也可选从函数 返回一个值 .使用字面意义上的语句~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 例7.7 使用字面意义上的语句 ...
- (简单) POJ 1797 Heavy Transportation,Dijkstra。
Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can no ...
- (简单) HDU 3308 LCIS,线段树+区间合并。
Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...
- mvc中上传图片到指定文件夹中
前台: @using (Html.BeginForm("AddImg", "UpFileImg", FormMethod.Post, new { enctype ...
- 扩展对EasyUI的校验规则
var myReg = RegExp(/[(\*)(\|)(\\)(\:)(\")(\/)(\<)(\>)(\?)]+/); $.extend($.fn.validatebox. ...