GCD

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2742    Accepted Submission(s): 980

Problem 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
/*
HDU 5726 GCD 区间GCD=k的个数 problem:
给你一列数字,然后是m个询问,每次询问区间[l,r]的gcd以及整个序列中有多少个区间的gcd与之相等 solve:
第一个可以通过线段树或者类rmq的方法来解决.但是求区间的个数就不知怎么弄了- -
最开始想的是每次询问求出值之后用 二分+枚举右端点 的思路来查找有多少个区间的
但是发现整个是递减的,感觉很难确定区间的大小,卒. 看别人题解才发现可以预处理,就一个左端点l而言,[l+1,n]中的到l的区间gcd是递减的.
例:
GCD[l,j] = 4,GCD[l,j+1] = 4,GCD[l,j+2] = 2
感觉题解相当于枚举以l为左点的所有区间GCD值,然后二分到当前GCD值的最右点,计算出区间的个数 因此会涉及很多次区间GCD查询,用线段树的话超时,用RMQ实现O(1)的查询AC
至于时间复杂度, 并不会算QAQ hhh-2016-08-15 21:35:11
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
int a[maxn];
int m[maxn];
int dp[maxn][20]; ll gcd(ll a,ll b)
{
if(b==0) return a;
else return gcd(b,a%b);
}
void iniRMQ(int n,int c[])
{
m[0] = -1;
for(int i = 1; i <= n; i++)
{
m[i] = ((i&(i-1)) == 0)? m[i-1]+1:m[i-1];
dp[i][0] = c[i];
}
for(int j = 1; j <= m[n]; j++)
{
for(int i = 1; i+(1<<j)-1 <= n; i++)
dp[i][j] = gcd(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
} int RMQ(int x,int y)
{
int k = m[y-x+1];
return gcd(dp[x][k],dp[y-(1<<k)+1][k]);
} map<int,ll>mp; void ini(int n)
{
mp.clear();
for(int i = 1;i <= n;i++)
{
int now = a[i],j = i;
while(j <= n)
{
int l = j,r = n;
while(l < r)
{
int mid = (l+r+1) >> 1;
if(RMQ(i,mid) == now)
l = mid;
else
r = mid-1;
}
mp[now] += (ll)(l-j+1);
j = l+1;
now = RMQ(i,j);
}
}
} int main()
{
int T,n,m;
int cas = 1;
// freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
printf("Case #%d:\n",cas++);
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
}
iniRMQ(n,a);
ini(n);
scanf("%d",&m);
int a,b;
for(int i =1; i <= m; i++)
{
scanf("%d%d",&a,&b);
int ans1 = RMQ(a,b);
printf("%d %I64d\n",ans1,mp[ans1]);
}
}
return 0;
}

  

HDU 5726 GCD 区间GCD=k的个数的更多相关文章

  1. HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)

    题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...

  2. hdu 5726 GCD GCD+线段树+区间预处理+map

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  3. HDU 5726 GCD (RMQ + 二分)

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

  4. HDU 5726 GCD(RMQ+二分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...

  5. HDU 5726 GCD

    传送门 GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  6. HDU 5726 GCD(DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...

  7. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  8. HDU-1695 GCD(求一个区间内与一个数互质的个数)

    题意: 给你一个T,是样例的个数,接下来是五个数l1,r1,l2,r2,k  前四个数代表两个区间(l1,r1),(l2,r2)这个题l1=1,l2=1; 取x1属于(1,r1),x2属于(1,r2) ...

  9. GCD (hdu 5726)

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

随机推荐

  1. 团队作业4——第一次项目冲刺(Alpha版本)11.18

    a. 提供当天站立式会议照片一张 举行站立式会议,讨论项目安排: 整理各自的任务汇报: 全分享遇到的困难一起讨论: 讨论接下来的计划: b. 每个人的工作 (有work item 的ID) 1.前两天 ...

  2. 关于使用栈将一般运算式翻译为后缀表达式并实现三级运算的方法及实例(cpp版)

    #include <iostream> #include <stack> #include <vector> #include <string> #de ...

  3. 自主学习之RxSwift(一) -----Driver

    对于RxSwift,我也是初学者,此系列来记录我学习RxSwift的历程! (一) 想必关于Drive大家一定在RxSwift的Demo中看到过,也一定有些不解,抱着一起学习的态度,来了解一下Driv ...

  4. Linux安装mongodb总结

    由于自己的博客上线部署时需要用到mongodb来存储图片文件,所以先在本地电脑上安装了mongodb做测试,由于之前没接触过mongodb,所以安装过程中遇到了各种小问题,折腾了好久终于安装好并成功启 ...

  5. SourceTree 实现 git flow 流程

    为什么使用 git 和 git flow,这篇文章 深入理解学习Git工作流 的内容相信能够给你一个完整的答案. 我们以使用SVN的工作流来使用git有什么不妥? git 方便的branch在哪里,团 ...

  6. 北亚关于HP EVA4400/6400/8400/P6000的数据恢复解决方案

    [引言]本文档建立在针对HP EVA的大量测试性研究基础上,所有的细节几乎均为对EVA的破译型研究,目前全球范围内尚未发现类似资料,故可能表述方式和结论并不精确,仅为参考之用.我们公司为研究HP EV ...

  7. EasyUI中DataGrid隔行改变背景颜色。

    <table id="dg" class="easyui-datagrid" style="width: 1000px; height: 300 ...

  8. RocketMQ(二):RPC通讯

    匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...

  9. AngularJS1.X学习笔记8-自定义指令(上)

    AngulaJS的指令是一种非常强大的特性,一个ng-repeat就能让我们非常方便的展示一个数据列表,指令相当于是一个组件,为我们将一些东西封装起来了,提供了复用的可能性.个人认为自定义指令还是比较 ...

  10. 新概念英语(1-131)Don't be so sure

    Lesson 131 Don't be so sure! 别那么肯定! Listen to the tape then answer this question. What's the problem ...