HDU 5381 The sum of gcd (tricks, MO team algorithm)

Source: Internet
Author: User
Tags gcd

I thought a little water, O (N2) can still be, after all, only 3 test case. I don't want to die.

1#include <bits/stdc++.h>2 #defineMAX (x, y) (() > (y)? (X): (Y))3 #defineMIN (x, Y) ((×) < (y)? (X): (Y))4 #definePII pair<int,int>5 #defineINF 0x7f7f7f7f6 #defineLL Long Long7 using namespacestd;8 Const intn=10005;9 structnodeTen { One     intL, R, ans; A }que[n]; - structNode1 - { the     intll, RR, GCD; - Node1 () {}; -Node1 (intllintRrintGCD): LL (LL), RR (RR), GCD (GCD) {}; -}reg[n][ -]; +  - int_GCD (intAintb) {returnb = =0? A: _GCD (b, a%b);} +  A intSeq[n], N, Q, pre[n]; at intCnt[n]; - voidpre_cal () - { -memset (CNT,0,sizeof(CNT)); -reg[n][0]=node1 (N,n,seq[n]);//one last special treatment -cnt[n]++; in      for(inti=n-1; i>=1; i--) -     { to         intR=i, gcd=Seq[i], tmp; +          for(intj=0; j<cnt[i+1]; J + +) -         { theNode1 &t=reg[i+1][j]; *tmp=_gcd (GCD, T.GCD); $             if(TMP!=GCD) reg[i][cnt[i]++]=Node1 (i, R, GCD);Panax NotoginsengGcd=tmp; -R=T.RR; the         } +reg[i][cnt[i]++]=Node1 (i, R, GCD); A     } the } +  - voidcal () $ { $      for(intI=1; i<=n; i++) -     { -pre[i-1]=0; thepre[i]=Seq[i]; -         intp=0;Wuyi          for(intj=i+1; j<=n; J + +) the         { -             if(J&GT;REG[I][P].RR) p++; Wupre[j]=REG[I][P].GCD; -pre[j]+=pre[j-1]; About         } $          for(intj=1; j<=q; J + +) -             if(I>=que[j]. L && i<=Que[j]. R) -QUE[J].ANS+=PRE[QUE[J]. r]-pre[i-1]; -     } A      for(intI=1; i<=q; i++) printf ("%d\n", Que[i].ans); + } the intMain () - { $     //freopen ("Input.txt", "R", stdin); the     intT, L, R; theCin>>T; the      while(t--) the     { -scanf"%d", &n); in          for(intI=1; i<=n; i++) scanf ("%d", &seq[i]); the  thescanf"%d", &q); About          for(intI=1; i<=q; i++) the         { thescanf"%d%d", &que[i]. L, &Que[i]. R); theque[i].ans=0; +         } - pre_cal (); the cal ();Bayi     } the  the     return 0; -}
tle Code

HDU 5381 The sum of gcd (tricks, MO team algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.