链接: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. html中input type=file 改变样式

    <style> #uploadImg{ font-size:12px; overflow:hidden; position:absolute} #file{ position:absolu ...

  2. js实现归并排序

    function merge(s_arr, d_arr, start, middle, end){ var s_temp = start; var m_temp = middle+1; var tem ...

  3. 【优先队列】【最近连STL都写不出来了/(ㄒoㄒ)/~~】hdu_5360/多校#6_1008

    题意:就是让你写出一个邀请朋友的顺序,朋友答应一起出去玩的条件是除他以外所有同意出去玩的人数要在[ l[i], r[i] ]范围内,否则他就不答应. 分析:这题比赛的时候想麻烦了,其实只要在左边界满足 ...

  4. [算法练习] UVA 10420 - List of Conquests?

    UVA Online Judge 题目10420 - List of Conquests 问题描述: 题目很简单,给出一个出席宴会的人员列表,包括国籍和姓名(姓名完全没用).统计每个国家有多少人参加, ...

  5. MyBatis(3.2.3) - Configuring MyBatis using XML, Settings

    The default MyBatis global settings, which can be overridden to better suit application-specific nee ...

  6. Redis 命令 - Lists

    BLPOP key [key ...] timeout Remove and get the first element in a list, or block until one is availa ...

  7. Android:Xml(读取与存储)

    1.读取XML文件 参数xml是建含xml数据的输入流,List<Person> persons用于存储xml流中的数据. XmlPullParser类的几个方法:next(),nextT ...

  8. request对象实现请求转发

    request对象实现请求转发,请求转发指一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理.request对象提供了一个getRequestDispatcher方法,该方法返 ...

  9. Windows命令实现Sleep

    等待一分钟:ping 192.0.2.2 -n 1 -w 60000 > nul 等待一秒钟:ping 192.0.2.2 -n 1 > nul (ping一次需要一秒钟) -w 6000 ...

  10. C# WinForm打开IE浏览器并访问网址

    C# WinForm 打开浏览器并访问网址代码: System.Diagnostics.Process.Start("iexplore.exe", "http://kel ...