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. PHP 多线程、多进程

    多线程:PHP其实并不支持多线程,只是通过一些扩展或者socket方式伪装成多线程,实质不是的.在PHP 5.3 以上版本,使用 pthreads PHP扩展,可以使PHP真正地支持多线程:或者使用 ...

  2. (简单) POJ 1961 Period,扩展KMP。

    Description For each prefix of a given string S with N characters (each character has an ASCII code ...

  3. jquery 改变变量出现值不同步

    出现问题的代码 var unc = 0; $.get( 'index.php', 'data=1', function(res) { unc=1; } ); alert(nuc); 这样的话,不管aj ...

  4. LPC1788的spi使用

    #ifndef __SPI_H_ #define __SPI_H_ #include "common.h" #include "delay.h" // cs p ...

  5. FZU 2095 水面高度

    一共六种情况.手算即可. #include<cstdio> #include<cstring> #include<cmath> int T; double a,b, ...

  6. 分布式数据库Google Spanner原理分析

    Spanner 是Google的全球级的分布式数据库 (Globally-Distributed Database) .Spanner的扩展性达到了令人咋舌的全球级,可以扩展到数百万的机器,数已百计的 ...

  7. 基于libUSB的USB设备固件更新程序(下载数据)(转)

    源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...

  8. sqlserver 批量删除相同前缀名的表

    ) DECLARE tmpCur CURSOR FOR SELECT name FROM sys.objects WHERE TYPE='U' AND name LIKE N'HSUPA%' OPEN ...

  9. MVC笔记3:JQuery AutoComplete组件

    1.引入以下js和css <link href="@Url.Content("~/Content/Site.css")" rel="styles ...

  10. Python模块学习:threading 多线程控制和处理

    Reference:http://python.jobbole.com/81546/ threading.Thread Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有 ...