The sum of gcd

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

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
题意:一句话题意;
思路:首先莫队离线处理,式必须的;
   然后重点就在于如何更新那个变化的值;
   根据取模的性质,每次取模最少/2;
   gcd也是同理;
   也就是说区间的gcd不同数的个数最多不超过log(n)个;
   这样就可以更新了;
   开始我写了一个RMQ,每次二分查找每一段,超时了;
   然后学到一个预处理的姿势;
   预处理见代码init那段;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e4+,M=4e6+,inf=;
const ll INF=1e18+,mod=1e9+; /// 数组大小 int a[N];
int n,pos[N],k;
vector<pair<int,int> >l[N],r[N];
struct is
{
int l,r,now;
bool operator <(const is &b)const
{
if(pos[l]!=pos[b.l])
return pos[l]<pos[b.l];
return r<b.r;
}
}p[N];
ll out[N],ans;
void prex(int L,int R,int flag)
{
ll sum=;
int p=L;
for(int i=;i<l[L].size();i++)
{
int k=min(R,l[L][i].first);
if(k>=p)
sum+=1LL*(k-p+)*l[L][i].second;
if(k>=R)break;
p=k+;
}
if(flag==)ans+=sum;
else ans-=sum;
} void nexx(int L,int R,int flag)
{
ll sum=;
int p=R;
for(int i=;i<r[R].size();i++)
{
int k=max(r[R][i].first,L);
if(p>=k)
sum+=1LL*(p-k+)*r[R][i].second;
if(k<=L)break;
p=k-;
}
if(flag==)ans+=sum;
else ans-=sum;
}
void init()
{
for(int i=;i<=n;i++)
l[i].clear(),r[i].clear();
ans=;
// 预处理l
l[n].push_back(make_pair(n,a[n]));
for(int i=n-;i>=;i--)
{
int g=a[i];
int p=i;
for(int j=;j<l[i+].size();j++)
{
int k=__gcd(g,l[i+][j].second);
if(g!=k)l[i].push_back(make_pair(p,g));
g=k;
p=l[i+][j].first;
}
l[i].push_back(make_pair(p,g));
}
//预处理r
r[].push_back(make_pair(,a[]));
for(int i=;i<=n;i++)
{
int g=a[i];
int p=i;
for(int j=;j<r[i-].size();j++)
{
int k=__gcd(g,r[i-][j].second);
if(g!=k)r[i].push_back(make_pair(p,g));
g=k;
p=r[i-][j].first;
}
r[i].push_back(make_pair(p,g));
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
k=sqrt(n);
for(int i=; i<=n; i++)
scanf("%d",&a[i]),pos[i]=(i-)/k+;
init();
int q;
scanf("%d",&q);
for(int i=; i<=q; i++)
scanf("%d%d",&p[i].l,&p[i].r),p[i].now=i;
sort(p+,p++q);
int L=,R=;
for(int i=; i<=q; i++)
{
while(L<p[i].l)
{
prex(L,R,);
L++;
}
while(L>p[i].l)
{
L--;
prex(L,R,);
}
while(R>p[i].r)
{
nexx(L,R,);
R--;
}
while(R<p[i].r)
{
R++;
nexx(L,R,);
}
out[p[i].now]=ans;
}
for(int i=; i<=q; i++)
printf("%lld\n",out[i]);
}
return ;
}
 

hdu 5381 The sum of gcd 莫队+预处理的更多相关文章

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

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

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

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

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

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

  4. 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 ...

  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. hdu5381 The sum of gcd]莫队算法

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

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

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

  9. hdu 5381 The sum of gcd

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

随机推荐

  1. IDA 7.0在Mojava更新后打不开的问题

    Mac升级到mojava后,ida 7.0打不开了. 上述是两种典型的窗口,不过不论是出现什么样的弹窗,如果是在升级之后出现的,都要试一下下面的解决办法.因为IDA7.0版本流出的比较多,虽然这个已经 ...

  2. linux命令:linux权限管理命令

    权限管理命令   文件的权限只有你两个人可以更改,一个是root,一个是文件所有者. 命令名称:chmod 命令英文原意:change the permissions mode of a  file ...

  3. 【设置】Nginx配置文件具体配置解释

    #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...

  4. svn回滚到某一版本

    svn回滚到某一版本 (1)在代码文件夹或vs中show log,查看历史,记住想要回滚到的版本号如1000 (2)新建文件夹,右击svn checkout,在revision中输入版本号1000

  5. Object之总结(一)

    一.Object类中一共有12个方法.一个私有方法,两个保护方法,9个公共方法.另外还有一个静态代码块. 1.registerNatives方法.私有静态本地无参数无返回值. 2.finalize方法 ...

  6. ServiceStack DotNet Core前期准备

    下载DotNet Core SDK 下载地址:https://dotnet.microsoft.com/download. 安装完成之后通过cmd的命令行进行确认. 官方自带的cmd比较简陋,可以用c ...

  7. 全文搜索引擎ElasticSearch学习记录:mac下安装

    最近开发组培训了ElasticSearch,准备开展新项目,我也去凑了下热闹,下面把学习过程记录一下. 一.安装 1.环境需要jdk1.8; 2.下载:http://www.elastic.co/do ...

  8. 修練營ASP.NET]淺談多層式架構 (Multi Tiers)

    從需求談起 我們舉個小例子來理解一般的方式與多層的方式有何不同 假設:我需要顯示最近三個月內,所有營業員的銷售金額成績排名 一般的做法: 在一個畫面中,拉個GridView,一個SqlDataSouc ...

  9. JS多重判断 / ES6 includes

    Array.includes ()  判断数组是否包含某个元素 直接返回true或者false表示是否包含元素,对NaN一样能有有效 const arr = ['1', '2', 'a', 'b' , ...

  10. Mocha describe 生命周期

    1 describe('test', function() { 2 // 在本测试块的所有测试用例之前执行且仅执行一次 3 before(function() { 4 5 }); 6 // 在本测试块 ...