首页 > 代码库 > bzoj 4052: [Cerc2013]Magical GCD

bzoj 4052: [Cerc2013]Magical GCD

bzoj4488的双倍经验!!

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 inline LL ra()
 5 {
 6     LL x=0; char ch=getchar();
 7     while (ch<0 || ch>9) ch=getchar();
 8     while (ch>=0 && ch<=9) {x=x*10+ch-0;ch=getchar();}
 9     return x;
10 }
11 
12 const int maxn=100005;
13 
14 struct node{
15     LL num; int x;
16     bool operator < (const node &y) const {return num<y.num||num==y.num && x<y.x;}
17 }f[2][maxn];
18 LL a[maxn];
19 int n;
20 
21 LL gcd(LL x, LL y) {return y==0?x:gcd(y,x%y);}
22 
23 int main()
24 {
25     int T=ra();
26     while (T--)
27     {
28         n=ra(); 
29         for (int i=1; i<=n; i++) a[i]=ra();
30         int s1=0,s2=0;
31         int p=0,q=1;
32         LL ans=0; node d;
33         for (int i=1; i<=n; i++)
34         {
35             for (int j=1; j<=s1; j++) f[p][j].num=gcd(a[i],f[p][j].num);
36             d.num=a[i]; d.x=i; s1++; f[p][s1]=d;
37             sort(f[p]+1,f[p]+s1+1);
38             s2=0;
39             for (int j=1; j<=s1; j++)
40                 if (f[p][j].num!=f[p][j-1].num)
41                     f[q][++s2]=f[p][j];
42             for (int j=1; j<=s2; j++) ans=max(ans,f[q][j].num*(LL)(i-f[q][j].x+1));
43             p^=1; q^=1; s1=s2;
44         }
45         printf("%lld\n",ans);
46     }
47     return 0;
48 }

 

bzoj 4052: [Cerc2013]Magical GCD