题目链接:

GCD

Time Limit: 10000/5000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

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
 
题意:
 
给含有n个数的序列,现在询问[l,r]的gcd是多少,同时还要求与gcd相同的区间有多少个;
 
思路:
 
静态区间可以用rmq,先预处理就可以O(1)的回答询问了;还有就是询问区间的个数,可以先预处理出所有的gcd,用map存下来,预处理的时候枚举左端点,由于gcd是单调递减的,所以可以分层后二分右端点,找到每层的长度,加到map中,由于一个数的因子个数不超过log(n)所以层数比较少才可以这样处理,写的线段树t了,看来静态区间查询还是rmq好;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=500+10;
const double eps=1e-10; int a[N],n,d[N][20];
map<int,LL>mp; int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
} inline void rmq()
{
for(int i=1;i<=n;i++)d[i][0]=a[i];
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
d[i][j]=gcd(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
}
return ;
}
inline int query(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)k++;
return gcd(d[l][k],d[r-(1<<k)+1][k]);
}
inline void Init()
{
mp.clear();
For(i,1,n)
{
int pos=i;
while(pos<=n)
{
int le=query(i,pos);
int l=pos,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(query(i,mid)<le)r=mid-1;
else l=mid+1;
}
mp[le]+=(LL)(r-pos+1);
pos=r+1;
}
}
}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
printf("Case #%d:\n",++Case);
read(n);
For(i,1,n)read(a[i]);
rmq();
Init();
int q;
read(q);
int l,r;
while(q--)
{
read(l);read(r);
int ans=query(l,r);
printf("%d ",ans);
print(mp[ans]);
}
} return 0;
}

  

hdu-5726 GCD(rmq)的更多相关文章

  1. HDU 5726 GCD (RMQ + 二分)

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

  2. HDU 5726 GCD 区间GCD=k的个数

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

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

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

  4. HDU 5726 GCD(RMQ+二分)

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

  5. hdu 5726 GCD 暴力倍增rmq

    GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...

  6. HDU 5726 GCD(ST&RMQ)

    题目链接 GCD 先ST倍增预处理,f[i][j]表示从i开始(包含第i个数)的连续2^j个数的最大公约数. 这样就可以在O(1)内询问得到a[l]到a[r]之间的所有数的最大公约数的值. 然后对于每 ...

  7. HDU 5726 GCD(DP)

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

  8. HDU 5726 GCD (2016 Multi-University Training Contest 1)

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

  9. HDU 5726 GCD

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

  10. hdu 5726 GCD 倍增+ 二分

    题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...

随机推荐

  1. HashMap 和 Hashtable 的同和不同

    综述 可以直接根据 hashcode 值判断两个对象是否相等吗?肯定是不可以的,因为不同的对象可能会生成相同的 hashcode 值.虽然不能根据 hashcode 值判断两个对象是否相等,但是可以直 ...

  2. js利用offsetWidth和clientWidth来计算滚动条的宽度

    原文: http://www.haorooms.com/post/js_scroll_width 参考: https://www.cnblogs.com/benxiaohai-microcosm/p/ ...

  3. angular 图片加载失败 情况处理? 如何在ionic中加载本地图片 ?

    1.angular 图片加载失败 情况处理 在directive中定义组件,在ng-src错误时,调用err-src app.directive('errSrc',function(){ return ...

  4. python(26)- 面向对象补充Ⅱ

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)判断obj是否是类 cls 的对象 class Foo(object): ...

  5. 【每日Scrum】第六天(4.16) TD学生助手Sprint1阶段性成果

    TD学生助手Sprint1阶段性成果(4.16) 任务看板 站立会议内容 组员 昨天 今天 困难 签到 刘铸辉 (组长) 和叶姐,静姐修改页面布局和图片显示,保证界面的亲切. 和大家一起做演示PPT, ...

  6. Audio原理图设计

    1.DMIC 1)当双MIC时,通过MIC上的Selection PIN脚PULL  U/D进行左右channel选择.

  7. VS快速生成JSON数据类

    1.复制Json数据 2.

  8. 在Linux中将php-fpm配置成服务的方法

    1.配置php-fpm.conf vi /usr/local/php/etc/php-fpm.conf php-fpm.pid 目录必须指向:/usr/local/php/var/run/php-fp ...

  9. 使用Caffe完成图像目标检测 和 caffe 全卷积网络

    一.[用Python学习Caffe]2. 使用Caffe完成图像目标检测 标签: pythoncaffe深度学习目标检测ssd 2017-06-22 22:08 207人阅读 评论(0) 收藏 举报 ...

  10. Kristen Grauman

    http://www.cs.utexas.edu/~grauman/ CV         Publications         Code           Data        Short ...