大神题解:

http://blog.csdn.net/u014800748/article/details/47680899

The sum of gcd

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 526    Accepted Submission(s): 226

Problem Description
You have an array A,the
length of A is n

Let f(l,r)=∑ri=l∑rj=igcd(ai,ai+1....aj)
 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

First line has one integers n

Second line has n integers Ai

Third line has one integers Q,the
number of questions

Next there are Q lines,each line has two integers l,r

1≤T≤3

1≤n,Q≤104

1≤ai≤109

1≤l<r≤n
 
Output
For each question,you need to print f(l,r)
 
Sample Input
2
5
1 2 3 4 5
3
1 3
2 3
1 4
4
4 2 6 9
3
1 3
2 4
2 3
 
Sample Output
9
6
16
18
23
10
 
Author
SXYZ
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn=10100;
typedef long long int LL; struct G
{
G(){}
G(int _id,LL _g):id(_id),g(_g){} int id;
LL g; void toString()
{
printf("id: %d g: %lld\n",id,g);
}
}; int n,a[maxn],Q;
vector<G> VL[maxn],VR[maxn];
struct Que
{
int L,R,id;
bool operator<(const Que& que) const
{
if(L!=que.L) return L<que.L;
return R<que.R;
}
}que[maxn]; void PreInit()
{
/// get Left Point
/// 以i为右端点,预处理出左边的段
for(int i=1;i<=n;i++)
{
VL[i].clear();
if(i==1)
{
VL[i].push_back(G(i,a[i]));
}
else
{
LL curg=a[i];int L=i;
for(auto &it : VL[i-1])
{
int g=__gcd(it.g,curg);
if(g!=curg) VL[i].push_back(G(L,curg));
curg=g; L=it.id;
}
VL[i].push_back(G(L,curg));
}
}
/// get Right Point
/// 以i为左端点,预处理出右边的段
for(int i=n;i>=1;i--)
{
VR[i].clear();
if(i==n)
{
VR[i].push_back(G(i,a[i]));
}
else
{
LL curg=a[i];int R=i;
for(auto &it : VR[i+1])
{
int g=__gcd(curg,it.g);
if(g!=curg) VR[i].push_back(G(R,curg));
curg=g; R=it.id;
}
VR[i].push_back(G(R,curg));
}
}
} /// 计算L,R之间的值
LL calu(int type,int L,int R)
{
LL ret=0;
if(type==0)
{
int tr=R;
for(auto &it : VL[R])
{
if(it.id>=L)
{
ret+=(tr-it.id+1)*it.g;
tr=it.id-1;
}
else
{
ret+=(tr-L+1)*it.g;
break;
}
}
}
else if(type==1)
{
int tr=L;
for(auto &it : VR[L])
{
if(it.id<=R)
{
ret+=(it.id-tr+1)*it.g;
tr=it.id+1;
}
else
{
ret+=(R-tr+1)*it.g;
break;
}
}
}
return ret;
} LL ans[maxn]; int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
PreInit(); scanf("%d",&Q);
for(int i=0,l,r;i<Q;i++)
{
scanf("%d%d",&l,&r);
que[i].L=l; que[i].R=r; que[i].id=i;
}
sort(que,que+Q); int L=1,R=0; LL ret=0;
for(int i=0;i<Q;i++)
{
while(R<que[i].R)
{
R++;
ret+=calu(0,L,R);
}
while(R>que[i].R)
{
ret-=calu(0,L,R);
R--;
}
while(L<que[i].L)
{
ret-=calu(1,L,R);
L++;
}
while(L>que[i].L)
{
L--;
ret+=calu(1,L,R);
}
ans[que[i].id]=ret;
} for(int i=0;i<Q;i++)
cout<<ans[i]<<endl;
}
return 0;
}

HDOJ 5381 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 莫队+预处理

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

  3. HDU-4676 Sum Of Gcd 莫队+欧拉函数

    题意:给定一个11~nn的全排列AA,若干个询问,每次询问给出一个区间[l,r][l,r],要求得出∑l≤i<j≤r  gcd(Ai,Aj)的值. 解法:这题似乎做的人不是很多,蒟蒻当然不会做只 ...

  4. Hdu5381-The sum of gcd(莫队)

    题意我就不说了   解析: 莫队,先预处理出以i为右端点的区间的gcd值,有一些连续的区间的gcd值是相同的,比如[j,i],[j+1,i],[j+2,i]的gcd值是相同的,我们可以把[j,j+2] ...

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

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

  6. hdu 4676 Sum Of Gcd 莫队+数论

    题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...

  7. HDU 5381 The sum of gcd (技巧,莫队算法)

    题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...

  8. HDU5381【莫队算法+区间GCD特性】

    前言: 主要最近在刷莫队的题,这题GCD的特性让我对莫队的使用也有了新的想法.给福利:神犇的一套莫队算法题 先撇开题目,光说裸的一个莫队算法,主要的复杂度就是n*sqrt(n)对吧,这里我忽略了一个左 ...

  9. hdu 5381 The sum of gcd

    知道对于一个数列,如果以x为左(右)端点,往右走,则最多会有log(a[x])个不同的gcd,并且有递减性 所以会分成log段,每一段的gcd相同 那我们可以预处理出对于每一个位置,以这个位置为左端点 ...

随机推荐

  1. UI概念体系要素

    结构.渲染.交互.数据. 要素.呈现.交互 1)UI(组成)要素:结构 2)布局: 3)渲染: 4)事件处理: 5)数据:

  2. Linux下 SpringBoot jar项目后台运行、查看、停用

    运行java jar: nohup java -jar **-0.0.1-SNAPSHOT.jar & 查看进程: 采用top或者ps aux命令.一般 如果后台是springboot,jar ...

  3. CAD参数绘制样条线(com接口)

    在CAD设计时,需要绘制样条线,用户可以设置样条线线重及颜色等属性. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定位置.详细说明如下: 参数 说明 DOUBL ...

  4. 04XML CSS

    XML CSS 1. XML CSS <?xml-stylesheet  href ="样式表的URI"  type= "text/css" ?> ...

  5. 第2节 mapreduce深入学习:14、mapreduce数据压缩-使用snappy进行压缩

    第2节 mapreduce深入学习:14.mapreduce数据压缩-使用snappy进行压缩 文件压缩有两大好处,节约磁盘空间,加速数据在网络和磁盘上的传输. 方式一:在代码中进行设置压缩 代码: ...

  6. Windows下Eclipse+PyDev安装Python开发环境

    .简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE方向版本.Java方向版本.C/C++方向版本.移动应用方向版本等诸多版本.除此之外,Eclipse还可以通过安装插件 ...

  7. ACdream 1063 字典树

    ACdream 1063 字典树 平衡树 神奇的cxlove有一颗平衡树,其树之神奇无法用语言来描述 OrzOrz. 这棵树支持3种操作: 1.加入一个数到树中,维护平衡树的合法性: 2.给一个数X, ...

  8. HDU 1800 hash 找出现最多次数的字符串的次数

    乘法hash: 这类hash函数利用了乘法的不相关性 int Hash(char *str){    int seed = 131 , value=0;    while(*str != '\0'){ ...

  9. [HDU2136] Largest prime factor(素数筛)

    传送门 题意 给出若干个数n(n<=1000000),求每个n的最大质因子的排名. 质数的排名:如果素数p是第k小的素数,那么p的排名就是k. 思路 乍一看不知道怎么搞. 其实可以想想我们怎么筛 ...

  10. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...