bzoj 4488: [Jsoi2015]最大公約數

來源:互聯網
上載者:User

標籤:nod   getch   return   觀察   efi   ret   fine   不能   std   

做這個題的時候蛋疼的想一個知道[l,r]的gcd那[l+1,r]的能不能快速算出來。

然後想了一下發現絲毫沒有道理。

於是又扒了題解。

神奇的題解說,一個序列的gcd最多有log個,突然就GG了

誒,遇到這樣的題還是多搞幾組資料觀察一下規律的好

 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     n=ra(); 26     for (int i=1; i<=n; i++) a[i]=ra();27     int s1=0,s2=0;28     int p=0,q=1;29     LL ans=0; node d;30     for (int i=1; i<=n; i++)31     {32         for (int j=1; j<=s1; j++) f[p][j].num=gcd(a[i],f[p][j].num);33         d.num=a[i]; d.x=i; s1++; f[p][s1]=d;34         sort(f[p]+1,f[p]+s1+1);35         s2=0;36         for (int j=1; j<=s1; j++)37             if (f[p][j].num!=f[p][j-1].num)38                 f[q][++s2]=f[p][j];39         for (int j=1; j<=s2; j++) ans=max(ans,f[q][j].num*(LL)(i-f[q][j].x+1));40         p^=1; q^=1; s1=s2;41     }42     printf("%lld\n",ans);43     return 0;44 }

 

bzoj 4488: [Jsoi2015]最大公約數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.