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. 《高级软件测试》web测试实践--12月30日记录

    考完数学,我们正式开始web测试实践的作业,今天,我们主要进行了方案的选择和人员的分工.任务计划和安排如上图所示. 任务进展:完成题目选择和人员分工: 遇到问题:暂无: 下一步任务:完成软件评测.用户 ...

  2. 《javascript设计模式与开发实践》阅读笔记(14)—— 中介者模式

    中介者模式 数个对象之间的通信全部委托一个中介者完成.适用于对象之间互相引用,关系错综复杂的情况. 什么情况下需要使用中介者模式 对象较多,且对象间会相互引用,当一个对象的某个状态改变时,得通知其他对 ...

  3. iOS 11 导航栏 item 偏移问题 和 Swift 下 UIButton 设置 title、image 显示问题

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  4. CSS揭秘(三)形状

    Chapter 3 1. 椭圆 椭圆的实现主要依靠 border-radius 属性,该属性确定边框切圆角的半径大小,可以指定数值 px,也可以使用百分比显示 而且该属性非常灵活,四个角可以分别设置 ...

  5. Python设计TFTP客户端

    #coding=utf-8 from socket import * from threading import Thread import struct def recvData(fileName, ...

  6. 学习phalcon框架按照官网手册搭建第一个项目注册功能

    中文手册官网:http://phalcon.ipanta.com/1.3/tutorial.html#bootstrap 官网提供http://www.tutorial.com项目源码github地址 ...

  7. Python扩展模块——调用WindowsAPI(pywin32的简单使用)

    这块使用的比较少,只用到了模拟键盘按键, 调用鼠标比较费事,是通过像素坐标实现的,如果没有特殊需求或万不得已不建议使用 import win32con import win32api win32api ...

  8. salesforce零基础学习(八十七)Apex 中Picklist类型通过Control 字段值获取Dependent List 值

    注:本篇解决方案内容实现转自:http://mysalesforceescapade.blogspot.com/2015/03/getting-dependent-picklist-values-fr ...

  9. tomcat 修改默认字符集

    找到connector节点,插入 disableUploadTimeout="true" useBodyEncodingForURI="true" URIEnc ...

  10. docker生态系统

    我的docker学习笔记6-docker生态   1.镜像即应用       代码构建.持续集成和持续交付        DaoCloud.Quay.IO 2.催生容器托管caas服务       基 ...