HDU 5726 GCD 区间GCD=k的个数
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2742 Accepted Submission(s): 980
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.
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).
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
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的个数的更多相关文章
- HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
- hdu 5726 GCD GCD+线段树+区间预处理+map
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- HDU 5726 GCD(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- HDU 5726 GCD
传送门 GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD Accepts: 64 Submissions: 535 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2 ...
- HDU-1695 GCD(求一个区间内与一个数互质的个数)
题意: 给你一个T,是样例的个数,接下来是五个数l1,r1,l2,r2,k 前四个数代表两个区间(l1,r1),(l2,r2)这个题l1=1,l2=1; 取x1属于(1,r1),x2属于(1,r2) ...
- GCD (hdu 5726)
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
随机推荐
- python自动发邮件
from email.header import Header from email.mime.text import MIMEText from email.utils import parsead ...
- css变化代码
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- Java看书学习笔记
1.POM:maven ,项目管理工具存放Jar包的文件2.mybatis-generator-core-1.3.2 生成文件 生成语句: java -jar mybatis-generator-co ...
- Angular 学习笔记 ( PWA + App Shell )
PWA (Progressive Web Apps) 是未来网页设计的方向. 渐进式网站. Angular v5 开始支持 pwa 网站 (所谓支持意思是说有一些 build in 的方法和规范去实现 ...
- Spring Security 入门(1-3-2)Spring Security - http元素 - intercept-url配置
http元素下可以配置登录页面,也可以配置 url 拦截. 1.直接配置拦截url和对应的访问权限 <security:http use-expressions="false" ...
- Python基础题
1. 执行Python脚本的两种方式: Chmod +x 脚本 ./脚本(路径的方式) Python 脚本 2. 简述位.字节的关系 一个字节=8位 3. 简述ASCII.unicode.utf-8/ ...
- Linux命令-权限
Linux命令权限 1.新建用户natasha,uid为1000, gid为555, 备注信息为"master" 2.修改natasha用户的家目录为/Natasha 3.查看 ...
- pandas.read_csv参数详解
读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/pandas-docs/stable/io.html 参 ...
- python的错误处理
一.python的错误处理 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错以及出错的原因. 在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数o ...
- 浅析Java的Frok/Join框架
一丶Fork/Join框架产生背景: 随着并发需求的不断提高和硬件的不断发展,程序并行执行仿佛就提上日程上来了,伟大的毛主席就说过:"人多力量大",所以如果一件事可以分配给多个人同 ...