码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 4488/4052 gcd

时间:2017-03-29 01:02:27      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:gcd   复杂   暴力   printf   二分   siri   nod   space   str   

思路:

一开始 我是想 对于固定的左端点 从左到右 最多有 log种取值  且单调递减  那不妨倍增预处理+二分GCD在哪变了.. 复杂度O(nlog^2n)

gcd最多log种取值..

好了我们可以暴力了...

复杂度O(nlogn)

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int cases,n,top,temp;
ll xx,ans;
struct Node{int id;ll gcd;}jy[33],al[33];
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main(){
        scanf("%d",&n),ans=top=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&xx),temp=0;
            jy[++top].gcd=0,jy[top].id=i;
            for(int j=1;j<=top;j++)jy[j].gcd=gcd(jy[j].gcd,xx);
            for(int j=1;j<=top;j++)if(jy[j].gcd!=jy[j-1].gcd)
                ans=max(ans,jy[j].gcd*(i+1-jy[j].id)),jy[++temp]=jy[j];
            top=temp;
        }
        printf("%lld\n",ans);
}

 

BZOJ 4488/4052 gcd

标签:gcd   复杂   暴力   printf   二分   siri   nod   space   str   

原文地址:http://www.cnblogs.com/SiriusRen/p/6637677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!