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. 几种不同风格的Toast

    一般情况下,我们使用Toast默认的风格就行了,只是有些时候为了达到我们自己想要的效果需要自定义一下,包括自定义显示的位置,显示的内容以及完全自定义里面的布局,代码如下: activity: pack ...

  2. ios 实现跳转到评价界面的两种方式

    要想在App内跳转到特定App的详情页或者评论页,首先需要获取到App的id.在 iTunes Connect网站上登陆之后,选择“我的App”,然后点击某个特定的App进入,在App信息的综合信息中 ...

  3. CFileDialog的使用方法简单介绍

    CFileDialog文件选择对话框的使用:首先构造一个对象并提供对应的參数,构造函数原型例如以下: CFileDialog::CFileDialog( BOOL bOpenFileDialog, L ...

  4. 《Linux内核修炼之道》 系列

    http://blog.csdn.net/fudan_abc/article/category/655796

  5. python学习笔记--基础语法

    等待用户输入 #!/usr/bin/python raw_input("\n\nPress the enter key to exit.") 简单的判断 #!/usr/bin/py ...

  6. 【Android】isEmpty()

    对字符串进行非空判断,可以一次性进行两种空值的判断.当传入的字符串等于null或者等于空字符串的时候这个方法都会返回true,从而不需要单独去判断这两种空值,再利用逻辑运算符连接起来.

  7. Pivotal Cloud Foundry学习笔记(1)

    PCF是一个PAAS平台 注册PCF账号 https://account.run.pivotal.io/sign-up 安装cf CLI 访问 https://console.run.pivotal. ...

  8. 自己写的demo---声明异常同时处理异常,或者继续抛出异常

    package exception; public class exception { public static void main(String args[]) { /*** * 不能对类型 ex ...

  9. xml--小结③DTD的基本语法(看懂即可)

    四.DTD的基本语法(看懂即可)1.DTD:Document Type Definition2.作用:约束XML的书写规范.3.DTD文件保存到磁盘时,必须使用UTF-8编码 4.如何引入外部的DTD ...

  10. Java-struts2 之值栈问题

    这里是根据一个小项目,将数据库的值查出来,然后在页面前台进行遍历的方法 放入值的几种方式: Struts2的三种存值取值的方式 值栈: 栈上下文: ActionContext: package com ...