GCD

Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l′,r′)(1≤l<r≤N)such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).

Input

The first line of input contains a number T, which stands for the number of test cases you need to solve.

The first line of each case contains a number N, denoting the number of integers.

The second line contains N integers, a1,...,an(0<ai≤1000,000,000).

The third line contains a number Q, denoting the number of queries.

For the next Q lines, i-th line contains two number , stand for the li,ri, stand for the i-th queries. 

Output

For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1).

For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l′,r′) such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar). 

Sample Input

1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4

Sample Output

Case #1:
1 8
2 4
2 4
6 1
 //2016.8.9
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath> using namespace std; typedef long long ll;
const int N = ;
int dp[N][];//d[i][j]表示从第i个数字开始向后2^j个数字这段区间内的gcd,具有递减性
map<int, ll> mp; void init_rmq(int n)//初始化dp,求出每段区间的gcd
{
for(int j = ; j < (int)log2(n)+; j++)
for(int i = ; i <= n; i++)
{
if(i+(<<j)- <= n)
dp[i][j] = __gcd(dp[i][j-], dp[i+(<<(j-))][j-]);
}
} int rmq(int l, int r)//查询
{
int k = (int)log2(r-l+);
return __gcd(dp[l][k], dp[r-(<<k)+][k]);
} int main()
{
int n, q, l, r, T, kase = ;
cin>>T;
while(T--)
{
printf("Case #%d:\n", ++kase);
cin>>n;
mp.clear();
for(int i = ; i <= n; i++)
scanf("%d", &dp[i][]);
init_rmq(n); //利用二分求具有相同gcd区间的数目
//-----------------------------------------------------------------------------------
for(int i = ; i <= n; i++)
{
int a = i, b = n, mid, tmp, vs;
while()
{
tmp = a;
vs = rmq(i, a);
while(a <= b)
{
mid = (a+b)>>;
if(rmq(i, mid)<vs) b = mid-;
else a = mid+;
}
mp[vs]+=1ll*(b-tmp+);
b = n;
if(a>b)break;
}
}
//------------------------------------------------------------------------------------ cin>>q;
while(q--)
{
scanf("%d%d", &l, &r);
int ans = rmq(l, r);
cout<<ans<<" "<<mp[ans]<<endl;
}
} return ;
}

HDU5726(RMQ&&二分)的更多相关文章

  1. *HDU3486 RMQ+二分

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  3. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  5. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. 玲珑杯 Round 19 B Buildings (RMQ + 二分)

    DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [ ...

  7. codeforces 487B B. Strip(RMQ+二分+dp)

    题目链接: B. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

  9. HDU 5726 GCD (RMQ + 二分)

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

随机推荐

  1. JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)

    JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...

  2. The 2014 ACM-ICPC Asia Regional Anshan

    继续复盘下一场Regional! [A]-_-/// [B]模拟(之前每次遇到模拟.暴搜都直接跳了,题目太长也是一个原因...下次是在不行可以尝试一下) [C]数论 互质.容斥? [D]数学推导(方差 ...

  3. Keil C动态内存管理机制分析及改进(转)

    源:Keil C动态内存管理机制分析及改进 Keil C是常用的嵌入式系统编程工具,它通过init_mempool.mallloe.free等函数,提供了动态存储管理等功能.本文通过对init_mem ...

  4. ibatis一对多 数据库设计及实现

    iBatis的多表关联. ibatis的表关联,和数据库语句无关,是在Java程序中,把若干语句的结果关联到一起.这种关联形式,虽然在大数据量时是很奢侈的行为,但是看起来很干净,用起来也很方便. 这里 ...

  5. iOS开发——获取当前屏幕显示的viewcontroller

    获取当前屏幕显示的viewcontroller,然后想怎么跳就怎么跳. - (UIViewController *)getCurrentVC { UIViewController *result = ...

  6. HTML 5终于定稿,八年后我们再一次谈谈怎么改变世界

    我们第一次谈论 HTML5 要改变世界大概是因为乔布斯,他坚持在 iOS 上不兼容 Flash,在 Adobe 统治多媒体开发的那个年代,这需要付出极大的勇气.这么多年过去了,虽然所有人都在谈论 HT ...

  7. Delphi2010生成GB2312字库乱码问题

    用Delphi2010做一个点阵字库软件,字库生成部分是从一个用Delphi2007做旧的程序里扣出来的.点阵字库软件完成后生成GB2312字库在LED控制卡上显示为乱码.知道Delphi版本高于20 ...

  8. ZOJ 3923 Handshakes

    水题. 算一下每个人和之前的人握手次数+之后的人和这个人握手次数.取最大值. #include<cstdio> #include<cstring> #include<cm ...

  9. IOS_FMDB有关字典、数组存储及获取问题

    http://blog.csdn.net/betterbb/article/details/25984455 FMDB存储字典或数组时会变成字符串存入sqlite里,但如果不将其转换成json格式存储 ...

  10. double和real型有什么区别 [

    DOUBLE是双精度浮点数REAL  是实数类型,他包括 DOUBLE,SINGLE等类型