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

Description

Give you a sequence of N(N \leq 100, 000) integers : a_{1},...,a_{n}(0 < a_{i} \leq 1000, 000, 000). There are Q (Q \leq 100, 000) queries. For each query l, r you have to calculate gcd(a_{l},,a_{l+1},...,a_{r}) and count the number of pairs(l’, r’) (1 \leq l < r \leq N)such thatgcd(a_{l’},a_{l’+1},...,a_{r’}) equal gcd(a_{l},a_{l+1},...,a_{r}).
 

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, a_{1},...,a_{n}(0 < a_{i} \leq 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 l_{i}, r_{i}, 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(a_{l},a_{l+1},...,a_{r}) and the second number stands for the number of pairs(l’, r’) such that gcd(a_{l’},a_{l’+1},...,a_{r’}) equal gcd(a_{l},a_{l+1},...,a_{r}). 
 

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
 

Source

2016 Multi-University Training Contest 1
 
 
 

题意:给一个数组a,大小为n,接下来有m个询问,每次询问给出l、r,定义f[l,r]=gcd(al,al+1,...,ar),问f[l,r]的值 和 有多少对(l',r')使得f[l',r']=f[l,r]。1<=l<=r<=n,题目中给的数据过大,不可直接使用dp方程。

思路:

  第一步,RMQ预处理一下,定义f[i][j]为:ai开始,连续2^j个数的最大公约数,所以f[1][0]=a[1],f[1][1]=gcd(a1,a2),f[1][2]=gcd(a1,a2,a3,a4)。递推即可。

  递推公式如下:

  1. f[i][0]=a[i];

  2. f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1])

  接着查询时就只需O(1)时间,如下:

  令k=log2(r-l+1),RMQ(l,r)=gcd(f[l][k],f[r-(1<<k)+1][k]);

  注:f[l][k] 和 f[r-(1<<k)+1][k]可能会有重叠,但不影响最终的gcd值。

  第二步二分法:我们可以枚举左端点 i 从1-n,对每个i,二分右端点,计算每种gcd值的数量,因为如果左端点固定,gcd值随着右端点的往右,呈现单调不增,这点很重要,比赛时没有想到,而且gcd值每次变化,至少除以2,所以gcd的数量为nlog2(n)种,可以开map<int,long long>存每种gcd值的数量,注意n大小为10万,所以有可能爆int。

#include<stdio.h>
#include<math.h>
#include<map>
using namespace std;
int f[][];
int a[];
int n,m;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
void rmq()
{
for(int i=; i<=n; i++) f[i][]=a[i];
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
f[i][j]=gcd(f[i][j-],f[i+(<<(j-))][j-]);
}
}
}
int RMQ(int l,int r)
{
int k=;
while((<<(k+))<=r-l+) k++;
return gcd(f[l][k],f[r-(<<k)+][k]);
}
map<int,long long> mp;
void setTable()
{
mp.clear();
for(int i=; i<=n; i++)
{
int g=f[i][],j=i;
while(j<=n)
{
int l=j,r=n;
while(l<r)
{
int mid=(l+r+)>>;
if(RMQ(i,mid)==g) l=mid;
else r=mid-;
}
mp[g]+=l-j+;
j=l+;
g=RMQ(i,j);
}
}
}
int main()
{
int t,l,r;
int cas=;
scanf("%d",&t);
while(t--)
{
printf("Case #%d:\n",cas++);
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
rmq();
setTable();
scanf("%d",&m);
for(int i=; i<m; i++)
{
scanf("%d%d",&l,&r);
int g=RMQ(l,r);
printf("%d %I64d\n",g,mp[g]);
}
}
return ;
}

HDU 5726 GCD (2016 Multi-University Training Contest 1)的更多相关文章

  1. HDU 5726 GCD(RMQ+二分)

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

  2. HDU 5726 GCD(DP)

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

  3. HDU 5726 GCD(ST&RMQ)

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

  4. HDU 5705 Clock(2016杭电女生专场1004)——角度追及问题

    题意是给出一个当前的时间和角度a,问从现在开始的下一个时针和分针形成角度a的时间是多少,时间向下取整. 分析:时针3600s走30°,故120s走1°,分针3600s走360°,故10s走1°,那么每 ...

  5. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

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

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

  7. ( 2018 Multi-University Training Contest 2)

    2018 Multi-University Training Contest 2) HDU 6311 Cover HDU 6312 Game HDU 6313 Hack It HDU 6314 Mat ...

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

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

  9. HDU 5726 GCD

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

随机推荐

  1. JQuery- 动画与效果

    这几天做网站,刚好用到! 1.基本效果 匹配元素从左上角开始变浓变大或缩小到左上角变淡变小 ①隐藏元素 除了可以设置匹配元素的display:none外,可以用以下函数 hide(speed,[cal ...

  2. Cocos2d-x 在缓存创建图片

    /* 加载图片资源到SpriteFrame缓存池*/     CCSpriteFrameCache *cache=CCSpriteFrameCache::sharedSpriteFrameCache( ...

  3. HTML里面Textarea换行总结

    近期碰到一个数据转来转去转到Textrea里面能否真正按行存放的问题,在这里总结一下:   问题描写叙述: 比方get数据到一个TextArea里面,如“AAA BBB”,想把这段文字在TextAre ...

  4. [Usaco2006 Nov]Corn Fields牧场的安排 壮压DP

    看到第一眼就发觉是壮压DP 然后就三进制枚举子集吧. 这题真是壮压入门好题... 对于dp[i][j] 表示第i行,j状态下前i行的分配方案数. 那么dp[i][j]肯定是从i-1行转过来的 那么由于 ...

  5. [Angular 2] DI in Angular 2 - 1

    Orgial aritial --> Link The problem with Angular 1 DI: Angular 2 DI: Solve the singletons problem ...

  6. MYSQL 源代码 学习

    http://blog.sina.com.cn/s/articlelist_1182000643_1_1.html http://blog.csdn.net/gao1738/article/detai ...

  7. 使用泛型定义一个可重用的Dao

    dao用来和数据库进行交互,一个项目中,可能有用户表,产品表等等,不可能为每一个表都建立一个dao,使用泛型可以实现通吃. UserDao.java public class UserDao < ...

  8. Bash判断文件是否存在

    #!/bin/bash if [ -f filename ]; then echo 'file exist' else echo 'file not exist' fi

  9. ADO.Net技术

    Connection对象 1.连接数据库 通过SqlConnection对象的State属性判断数据库的连接状态: public override ConnectionState State{ get ...

  10. 转-C# 操作 Excel 常见问题收集和整理

    经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个最基本的问题. 做开发这几年来,陆陆续续也接触过这样的需求,但因为 ...