莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n))。

  

  代码

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<vector>
  4. #include<algorithm>
  5. #define N 100000
  6. using namespace std;
  7. int n,q,s[N][],i,j,tmp,l,r;
  8. long long ans,Ans[N];
  9. vector<pair<int,int> > vec0[N],vec1[N];
  10. struct g
  11. {
  12. int l,r,t,id;
  13. }Q[N];
  14. bool cmp(g a,g b)
  15. {
  16. if (a.t==b.t)
  17. return a.r<b.r;
  18. return a.t<b.t;
  19. }
  20. int gcd(int a,int b)
  21. {
  22. if (b==) return a;
  23. return gcd(b,a%b);
  24. }
  25. int GCD(int a,int b)
  26. {
  27. int k;
  28. k=log2(b-a+);
  29. return gcd(s[a][k],s[b-(<<k)+][k]);
  30. }
  31. int ef(int i,int l,int r,int x)
  32. {
  33. int m;
  34. while (l<=r)
  35. {
  36. m=(l+r)>>;
  37. if (GCD(i,m)==x) l=m+;else r=m-;
  38. }
  39. return l;
  40. }
  41. int EF(int i,int l,int r,int x)
  42. {
  43. int m;
  44. while (l<=r)
  45. {
  46. m=(l+r)>>;
  47. if (GCD(m,i)==x) r=m-;else l=m+;
  48. }
  49. return r;
  50. }
  51. long long calc(int l,int r)
  52. {
  53. long long ans=;
  54. int len,t,i;
  55. if (l<r)
  56. {
  57. len=vec0[l].size();
  58. t=l;
  59. for (i=;i<len;i++)
  60. {
  61. ans+=1LL*(min(r,vec0[l][i].second)-t+)*vec0[l][i].first;
  62. t=vec0[l][i].second+;
  63. if (t>r) break;
  64. }
  65. }
  66. else
  67. {
  68. len=vec1[l].size();
  69. t=l;
  70. for (i=;i<len;i++)
  71. {
  72. ans+=1LL*(t-max(r,vec1[l][i].second)+)*vec1[l][i].first;
  73. t=vec1[l][i].second-;
  74. if (t<r) break;
  75. }
  76. }
  77. return ans;
  78. }
  79. void QL()
  80. {
  81. while (l<Q[i].l)
  82. {
  83. ans-=calc(l,r);
  84. l++;
  85. }
  86. while (l>Q[i].l)
  87. {
  88. l--;
  89. ans+=calc(l,r);
  90. }
  91. }
  92. void QR()
  93. {
  94. while (r<Q[i].r)
  95. {
  96. r++;
  97. ans+=calc(r,l);
  98. }
  99. while (r>Q[i].r)
  100. {
  101. ans-=calc(r,l);
  102. r--;
  103. }
  104. }
  105. int main()
  106. {
  107. int test;
  108. scanf("%d",&test);
  109. while (test--)
  110. {
  111. scanf("%d",&n);
  112. for (i=;i<=n;i++)
  113. {
  114. scanf("%d",&s[i][]);
  115. vec0[i].clear();
  116. vec1[i].clear();
  117. }
  118. for (i=n;i>=;i--)
  119. for (j=;j<=log2(n);j++)
  120. s[i][j]=gcd(s[i][j-],s[i+(<<(j-))][j-]);
  121.  
  122. for (i=;i<=n;i++)
  123. {
  124. l=i;r=n;
  125. while (l<=n)
  126. {
  127. tmp=GCD(i,l);
  128. l=ef(i,l,r,tmp);
  129. vec0[i].push_back(make_pair(tmp,l-));
  130. }
  131. }
  132.  
  133. for (i=n;i>=;i--)
  134. {
  135. l=;r=i;
  136. while (r>)
  137. {
  138. tmp=GCD(r,i);
  139. r=EF(i,l,r,tmp);
  140. vec1[i].push_back(make_pair(tmp,r+));
  141. }
  142. }
  143.  
  144. scanf("%d",&q);
  145. for (i=;i<=q;i++)
  146. {
  147. scanf("%d%d",&Q[i].l,&Q[i].r);
  148. Q[i].id=i;Q[i].t=Q[i].l/;
  149. }
  150. sort(Q+,Q++q,cmp);
  151. l=Q[].l;r=Q[].r;ans=;
  152. for (i=l;i<=r;i++) ans+=calc(i,r);
  153. Ans[Q[].id]=ans;
  154.  
  155. for (i=;i<=q;i++)
  156. {
  157. if (l<Q[i].l)
  158. {
  159. QR();QL();
  160. }
  161. else
  162. {
  163. QL();QR();
  164. }
  165. Ans[Q[i].id]=ans;
  166. }
  167. for (i=;i<=q;i++)
  168. printf("%I64d\n",Ans[i]);
  169. }
  170. }

hdu5381 The sum of gcd的更多相关文章

  1. hdu5381 The sum of gcd]莫队算法

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...

  2. 【HDU 5381】 The sum of gcd (子区间的xx和,离线)

    [题目] The sum of gcd Problem Description You have an array A,the length of A is nLet f(l,r)=∑ri=l∑rj= ...

  3. hdu 5381 The sum of gcd 莫队+预处理

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  4. hdu 4676 Sum Of Gcd 莫队+phi反演

    Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...

  5. HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)

    Given you a sequence of number a 1, a 2, ..., a n, which is a permutation of 1...n. You need to answ ...

  6. HDU 4676 Sum Of Gcd 【莫队 + 欧拉】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...

  7. hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  8. hdu 5381 The sum of gcd(线段树+gcd)

    题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...

  9. HDOJ 5381 The sum of gcd 莫队算法

    大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...

随机推荐

  1. 解读ClassLoader

    ClassLoader一个经常出现又让很多人望而却步的词,本文将试图以最浅显易懂的方式来讲解 ClassLoader,希望能对不了解该机制的朋友起到一点点作用.    要深入了解ClassLoader ...

  2. JQuery通过$(xxx...)返回对象

    var JQ = function () { return new JQ.prototype.init(); }; JQ.prototype.init = function () { }; JQ.pr ...

  3. 企业服务总线(ESB)

    思考: 1.ESB的定义到底是什么?是一款产品还是一种架构模式? 2.ESB有何实际用处? 定义ESB 对于企业服务总线(Enterprise Service Bus),目前还没有公认的定义,根据供应 ...

  4. Excel Sheet Column Number || leetcode

    很简单的26进制问题 int titleToNumber(char* s) { int sum=0,temp; char *p=s; while(*p!='\0'){ sum=sum*26+(*p-' ...

  5. Cocos2d-JS cc.DrawNode用法

    app.js var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { ///////////////////// ...

  6. [LeetCode]题解(python):061-Rotate list

    题目来源 https://leetcode.com/problems/rotate-list/ Given a list, rotate the list to the right by k plac ...

  7. Excel导入数据到Sql server 中出错:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项”

    从Excel导入数据到Sql server 时,由于表中的数据有的很长,导入时出现如下错误(如果数据不是很长,255内以内,则不会出现错误): 出错原因: SQL Server的导入导出为了确定数据表 ...

  8. AD834+表面声波滤波器调试小结

    AD834:带宽较宽,300M无压力,但是输出幅度小,当输入2VPPX2VPP的时候最大输出400mVPP.一个特别要注意的地方是输入走线尽量短,敷铜间距至少满足3倍线宽,不然隔直之后还会耦合信号进去 ...

  9. LeetCode Longest Palindrome

    原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...

  10. Oracle直方图导致SQL不走索引.

    在ITPUB 上看到一个帖子 http://www.itpub.net/thread-1875212-1-1.html 同一条SQL语句,只有查询条件不一样,查询返回的结果集都为0,一个走了全表扫描, ...