链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726

题意:有N(N <= 100,000),之后有Q(Q <= 100,000)个区间查询[l,r]。问ans1 = gcd(al,al+1,...ar) = ?,并且有多少组[l',r'] 的gcd值等于ans1?

思路:

对于求解ans1,由于gcd(a,b,c) = gcd( gcd(a,b),c) 所以可以使用ST表的思想,倍增DP求解区间的gcd值,时间复杂度为O(nlog(n)),之后RMQ查找区间[l,r]的gcd值时,也是和ST表类似;

如果求解个数?

注意到从某个点起的区间的gcd值的变化为非增的;并且每次变化减少的质因子值至少为2,所以个数不超过log(1e9)个;

这时对于从每一个点起使用二分右端点,递推左端点即可在log(n)时间内预处理出[l,n]的所有gcd值,累加到map中,之后O(1)即可;

好题:单调性是一个很好的性质。。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define A first
#define B second
#define MK make_pair
#define esp 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define clear0 (0xFFFFFFFE)
#define mod 1000000007
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = 0,f = 1;char ch = getchar();
while(ch <'0' || ch >'9'){ if(ch == '-') f = -1;ch=getchar(); }
while(ch >= '0' && ch <= '9'){ x = x*10 + ch - '0';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>9) out(a/10);
putchar(a%10+'0');
}
inline ll gcd(ll a,ll b){ return b == 0? a: gcd(b,a%b); }
const int maxn = 1e5 + 10;
int a[maxn],g[maxn][18];
void ST(int n)
{
rep1(i,1,n) g[i][0] = a[i];
for(int i = 1; i <= 17; i++){
for(int p = 1; p + (1<<i) <= n+1; p++){
g[p][i] = gcd(g[p][i-1],g[p+(1<<i-1)][i-1]);
}
}
}
int RMQ(int l,int r)
{
int len = log(1.*(r-l+1))/log(2);
return gcd(g[l][len],g[r-(1<<len)+1][len]);
}
map<int, ll> mp;
ll solve(int n)
{
mp.clear();
rep1(i,1,n){
for(int j = i;j <= n;j++){
int _gcd = RMQ(i,j), l = j, r = n, tmp = j;
while(l <= r){
int mid = l + r >> 1;
if(RMQ(j,mid) == _gcd) l = mid+1,tmp = mid;
else r = mid - 1;
}
mp[_gcd] += tmp - j + 1;
j = tmp;
}
}
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T, kase = 1;
scanf("%d",&T);
while(T--){
printf("Case #%d:\n", kase++);
int n, m;
read1(n);
rep1(i,1,n) read1(a[i]);
ST(n);
solve(n);
read1(m);
while(m--){
int l, r, aux;
read2(l,r);
aux = RMQ(l,r);
printf("%d %I64d\n",aux,mp[aux]);
}
}
return 0;
}

2016 Multi-University Training Contest 1 GCD RMQ+二分(预处理)的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. HDU 5726 GCD (RMQ + 二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...

  3. 2016 Multi-University Training Contest 1 GCD【RMQ+二分】

    因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺.废话不多说了,补题... update////2 ...

  4. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  5. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  6. [CFGym101028] 2016 Al-Baath University Training Camp Contest-1

    比赛链接:http://codeforces.com/gym/101028/ 由于实习,几乎没有时间刷题了.今天下午得空,断断续续做了这一套题,挺简单的. A.读完题就能出结果. /* ━━━━━┒ギ ...

  7. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  8. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  9. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

随机推荐

  1. Android(java)学习笔记96:如何改变spinner系统自带的字体和颜色

    1.首先我们要知道spinner系统自带字体和颜色本质: 原生的Spring 控件是无法更改字体和颜色的... 从下面的代码可以看出...红色的标注显示使用的是Android默认的布局.. Spinn ...

  2. XML 格式转JSON 格式

    #import <Foundation/Foundation.h> #pragma GCC diagnostic push #pragma GCC diagnostic ignored & ...

  3. LeetCode 203

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  4. linux 第一次获得root权限

    开机进入桌面,ctrl+alt+T打开终端————在此时终端显示的是 用户名@电脑名:-$   表示普通用户   在此处输入:sudo passwd root   此时提示———— [sudo] pa ...

  5. java的回忆录

    封装的三步骤:(1)加属性(成员变量.全局变量.域field)用private来修饰(2)为对应的属性生成共有的setter.getter方法(3)在对应的setter的方法中可以根据需要加入对应的验 ...

  6. 关于Winform发布时,资源文件缺失的解决方案

    今天和大家分享一下,我这几天一直困惑的问题,今天不经意间解决了这个问题,接下来描述一下这个问题: 问题: 最近公司给人家做二次开发,是C/S结构的,我在做Winform时发现,当我选择一个项目发布时, ...

  7. NVelocity 实现简单的 CIUD

    1, NVelocity 是 一般处理程序.ashx 和 前台页面模板的桥梁.  2,我们现在建立一个简单的查询:   A,新建项目,把NVelocity.dll拉入项目中,并添加对其引用 B,新建C ...

  8. SharpZipLib 压缩后传输给第三方平台无法识别问题

    问题描述:在项目中需要将文件压缩然后传输给三方进行彩信发送,使用SharpZipLib 进行压缩,原先使用J#进行压缩处理,但是用SharpZipLib压缩后的zip文件传输过去之后,总会报发送失败. ...

  9. JDBC(用Eclipse操作数据库Oracle)的基础操作集合

    JDBC: JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编 ...

  10. 第四十二篇、自定义Log打印

    1.在Xcode 8出来之后,需要我们去关闭多余的日志信息打印 2.在开发的过程中,打印调试日志是一项比不可少的工程,但是在iOS 10中NSLog打印日志被屏蔽了,就不得不使用自定义Log 3.去掉 ...