hdu5381 The sum of gcd
莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n))。
代码
- #include<cstdio>
- #include<cmath>
- #include<vector>
- #include<algorithm>
- #define N 100000
- using namespace std;
- int n,q,s[N][],i,j,tmp,l,r;
- long long ans,Ans[N];
- vector<pair<int,int> > vec0[N],vec1[N];
- struct g
- {
- int l,r,t,id;
- }Q[N];
- bool cmp(g a,g b)
- {
- if (a.t==b.t)
- return a.r<b.r;
- return a.t<b.t;
- }
- int gcd(int a,int b)
- {
- if (b==) return a;
- return gcd(b,a%b);
- }
- int GCD(int a,int b)
- {
- int k;
- k=log2(b-a+);
- return gcd(s[a][k],s[b-(<<k)+][k]);
- }
- int ef(int i,int l,int r,int x)
- {
- int m;
- while (l<=r)
- {
- m=(l+r)>>;
- if (GCD(i,m)==x) l=m+;else r=m-;
- }
- return l;
- }
- int EF(int i,int l,int r,int x)
- {
- int m;
- while (l<=r)
- {
- m=(l+r)>>;
- if (GCD(m,i)==x) r=m-;else l=m+;
- }
- return r;
- }
- long long calc(int l,int r)
- {
- long long ans=;
- int len,t,i;
- if (l<r)
- {
- len=vec0[l].size();
- t=l;
- for (i=;i<len;i++)
- {
- ans+=1LL*(min(r,vec0[l][i].second)-t+)*vec0[l][i].first;
- t=vec0[l][i].second+;
- if (t>r) break;
- }
- }
- else
- {
- len=vec1[l].size();
- t=l;
- for (i=;i<len;i++)
- {
- ans+=1LL*(t-max(r,vec1[l][i].second)+)*vec1[l][i].first;
- t=vec1[l][i].second-;
- if (t<r) break;
- }
- }
- return ans;
- }
- void QL()
- {
- while (l<Q[i].l)
- {
- ans-=calc(l,r);
- l++;
- }
- while (l>Q[i].l)
- {
- l--;
- ans+=calc(l,r);
- }
- }
- void QR()
- {
- while (r<Q[i].r)
- {
- r++;
- ans+=calc(r,l);
- }
- while (r>Q[i].r)
- {
- ans-=calc(r,l);
- r--;
- }
- }
- int main()
- {
- int test;
- scanf("%d",&test);
- while (test--)
- {
- scanf("%d",&n);
- for (i=;i<=n;i++)
- {
- scanf("%d",&s[i][]);
- vec0[i].clear();
- vec1[i].clear();
- }
- for (i=n;i>=;i--)
- for (j=;j<=log2(n);j++)
- s[i][j]=gcd(s[i][j-],s[i+(<<(j-))][j-]);
- for (i=;i<=n;i++)
- {
- l=i;r=n;
- while (l<=n)
- {
- tmp=GCD(i,l);
- l=ef(i,l,r,tmp);
- vec0[i].push_back(make_pair(tmp,l-));
- }
- }
- for (i=n;i>=;i--)
- {
- l=;r=i;
- while (r>)
- {
- tmp=GCD(r,i);
- r=EF(i,l,r,tmp);
- vec1[i].push_back(make_pair(tmp,r+));
- }
- }
- scanf("%d",&q);
- for (i=;i<=q;i++)
- {
- scanf("%d%d",&Q[i].l,&Q[i].r);
- Q[i].id=i;Q[i].t=Q[i].l/;
- }
- sort(Q+,Q++q,cmp);
- l=Q[].l;r=Q[].r;ans=;
- for (i=l;i<=r;i++) ans+=calc(i,r);
- Ans[Q[].id]=ans;
- for (i=;i<=q;i++)
- {
- if (l<Q[i].l)
- {
- QR();QL();
- }
- else
- {
- QL();QR();
- }
- Ans[Q[i].id]=ans;
- }
- for (i=;i<=q;i++)
- printf("%I64d\n",Ans[i]);
- }
- }
hdu5381 The sum of gcd的更多相关文章
- hdu5381 The sum of gcd]莫队算法
题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...
- 【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= ...
- 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 ...
- hdu 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
- 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 ...
- HDU 4676 Sum Of Gcd 【莫队 + 欧拉】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...
- 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 ...
- hdu 5381 The sum of gcd(线段树+gcd)
题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...
- HDOJ 5381 The sum of gcd 莫队算法
大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...
随机推荐
- 解读ClassLoader
ClassLoader一个经常出现又让很多人望而却步的词,本文将试图以最浅显易懂的方式来讲解 ClassLoader,希望能对不了解该机制的朋友起到一点点作用. 要深入了解ClassLoader ...
- JQuery通过$(xxx...)返回对象
var JQ = function () { return new JQ.prototype.init(); }; JQ.prototype.init = function () { }; JQ.pr ...
- 企业服务总线(ESB)
思考: 1.ESB的定义到底是什么?是一款产品还是一种架构模式? 2.ESB有何实际用处? 定义ESB 对于企业服务总线(Enterprise Service Bus),目前还没有公认的定义,根据供应 ...
- Excel Sheet Column Number || leetcode
很简单的26进制问题 int titleToNumber(char* s) { int sum=0,temp; char *p=s; while(*p!='\0'){ sum=sum*26+(*p-' ...
- Cocos2d-JS cc.DrawNode用法
app.js var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { ///////////////////// ...
- [LeetCode]题解(python):061-Rotate list
题目来源 https://leetcode.com/problems/rotate-list/ Given a list, rotate the list to the right by k plac ...
- Excel导入数据到Sql server 中出错:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项”
从Excel导入数据到Sql server 时,由于表中的数据有的很长,导入时出现如下错误(如果数据不是很长,255内以内,则不会出现错误): 出错原因: SQL Server的导入导出为了确定数据表 ...
- AD834+表面声波滤波器调试小结
AD834:带宽较宽,300M无压力,但是输出幅度小,当输入2VPPX2VPP的时候最大输出400mVPP.一个特别要注意的地方是输入走线尽量短,敷铜间距至少满足3倍线宽,不然隔直之后还会耦合信号进去 ...
- LeetCode Longest Palindrome
原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...
- Oracle直方图导致SQL不走索引.
在ITPUB 上看到一个帖子 http://www.itpub.net/thread-1875212-1-1.html 同一条SQL语句,只有查询条件不一样,查询返回的结果集都为0,一个走了全表扫描, ...