莫队算法,预处理出每个数字往后的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的更多相关文章

  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. (转)js一道比较考验的题目

    转载下别人曾经出过的一道面试题,此题是他出的一套前端面试题中的最后一题,用来考核面试者的JavaScript的综合能力,很可惜到目前为止的将近两年中,几乎没有人能够完全答对,并非多难只是因为大多面试者 ...

  2. BLE-NRF51822教程19-Battery Service

    Battery Service是有关电池特性方面的服务,如果需要它,在初始化时将它加入到蓝牙协议栈. 如果通过ble_bas_battery_level_update(),电池电量将会通知,Batte ...

  3. CGROUP

    二:cgroup中的概念在深入到cgroup的代码分析之前.先来了解一下cgroup中涉及到的几个概念:1:cgroup: 它的全称为control group.即一组进程的行为控制.比如,我们限制进 ...

  4. wpf前端设计

    http://www.cnblogs.com/w-wanglei/archive/2016/03/14/5274298.html#_nav_0

  5. 纯Python包发布setup脚本编写示例

    如果你有多个模块需要发布,而它们又存在于多个包中,那么指定整个包比指定模块可能要容易地多.即使你的模块并不在一个包内,这种做法也行的通:你可以告诉Distutils从根包(root package)处 ...

  6. 7Z命令行

    7-Zip (A) 4.57 Copyright (c) 1999-2007 Igor Pavlov 2007-12-06 Usage: 7za <command> [<switch ...

  7. Tea---hdu5881(规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5881 题意: 现在有一壶水,体积在[L, R]范围内,现有两个空杯子,现想要把这壶水倒入这两个杯子中去 ...

  8. 解决libc.so.6: version `GLIBC_2.14' not found问题

    今天centos新机器上运行项目的时候出现题目所示的错误,搜索后发现是底层glibc 版本太低导致. strings /lib64/libc.so.6 |grep GLIBC_ 使用上面的命令发现 g ...

  9. JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一)概述及简单实例

    上文我们讲述了如何对 HTTP请求 的响应数据进行断言,以判断响应是否符合我们的预期,敬请参阅:JMeter学习-007-JMeter 断言实例之一 - 响应断言 那么我们如何获取 HTTP请求 响应 ...

  10. Java学习-023-Properties 类 XML 配置文件读取及写入源代码

    之前的几篇 Properties 文章已经讲述过了 Java 配置文件类 Properties 的基本用法,查看 JDK 的帮助文档时,也可看到在 Properties 类中还有两个方法 loadFr ...