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. CSS实现三角形方法一--rotate+relative

    方法说明:两个正方形,一个小的,一个大的,将大的正方向进行旋转,然后移动到小的正方形的合适位置,覆盖小正方形的一部分,使小正方形剩余部分为三角形,再把大正方形的背景色改为浏览器窗口的颜色. 用到知识: ...

  2. Javascript 浏览器探测

    1.根据浏览器的性能 var is = { ff: window.globalStorage, ie: document.all && !window.opera, ie6: !win ...

  3. python模块 ---logging模块

    摘要by crazyhacking: 与log4cxx一样,分为三个部分,logger, handler,formatter. 详细内容参考:1官网http://docs.python.org/2/h ...

  4. jsoncpp第二篇------API

    更多API参考jsoncpp头文件 1  jsoncpp的api简要说明 1,解析(json字符串转为对象) std::string strDataJson; Json::Reader JReader ...

  5. Servlet实现文件上传(多文件)(三)

    1.上传文件的页面fileUpload2.jsp <%@ page language="java" import="java.util.*" pageEn ...

  6. FZU 1397 保送

    网络流入门题. 源点到每一个学生连一条边,容量为1 每个学校到汇点连一条边,容量为L 符合要求的学生和学校之间连边,容量为1. 从源点到汇点的最大流就是答案. #include<cstdio&g ...

  7. leetcode--007 word break I

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+4AAAC5CAIAAAA55fI7AAAZa0lEQVR4nO3dPW7bQIMG4L2MT6B7+A

  8. leetcode--002 rpn

    package leetcode; import java.util.Stack; public class RPN { public static int evalRPN(String[] toke ...

  9. jQuery源码学习(2):选择器初窥

    选择器初窥 代码架构: jQuery选择器可以依照传入数据的类型分为五大类: 传入字符串:$("div"), $("#id"), $(".div1&q ...

  10. html 上传文件

    1.html代码 <form id="form1" action="TestYield" method="post" enctype= ...