HDU 5726 GCD(ST&RMQ)
题目链接 GCD
先ST倍增预处理,f[i][j]表示从i开始(包含第i个数)的连续2^j个数的最大公约数。
这样就可以在O(1)内询问得到a[l]到a[r]之间的所有数的最大公约数的值。
然后对于每个数a[i],以这个数为开头的所有子序列的最大公约数的不同值不会超过30个。
而且不同的值是满足单调递减的。
那么就可以二分查找然后把对应值的个数塞进map。
时间复杂度O(Nlog(N))
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define LL long long const int N = 100000 + 10;
const int A = 30 + 1; map <int, LL> mp;
int a[N];
int T;
int n, m;
int l, r;
int f[N][A];
int gcd(int a, int b){return b? gcd(b, a % b) : a;} inline int Solve(int l, int r){
int k = (int)log2((double)(r - l + 1));
return gcd(f[l][k], f[r - (1 << k) + 1][k]);
} void Work(){
rep(i, 1, n) f[i][0] = a[i];
rep(j, 1, 17) rep(i, 1, n) if ((i + (1 << j) - 1) <= n) f[i][j] =gcd(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
} void setTable(){
mp.clear();
rep(i, 1, n){
int g = f[i][0], j = i;
while (j <= n){
int l = j, r = n;
while (l < r){
int mid = (l + r + 1) >> 1;
if (Solve(l, mid) == g) l = mid; else r = mid - 1;
}
mp[g] += l - j + 1;
j = l + 1;
g = Solve(i, j);
}
}
}
int main(){ scanf("%d", &T);
rep(Case, 1, T){
printf("Case #%d:\n", Case);
scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i);
Work();
setTable();
scanf("%d", &m);
while (m--){
scanf("%d%d", &l, &r);
int g = Solve(l, r);
printf("%d %lld\n", g, mp[g]);
}
} return 0; }
HDU 5726 GCD(ST&RMQ)的更多相关文章
- hdu 1695 GCD(莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 5726 GCD(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Give y ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- HDU 1695 GCD (莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD(容斥)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...
- hdu 1695 GCD (欧拉函数、容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
随机推荐
- vsftpd服务安装与虚拟用户配置
vsftpd的全名是“Very secure FTP Daemon” 一.安装vsftpd安装db4-util用于生成认证文件 yum -y install db4-utils 安装vsftpd yu ...
- vue + axios---封装一个http请求
在使用vue开发时,官方推荐使用axios来请求接口 // axios官方地址 https://github.com/axios/axios 但是axios并不像 vue-resource 一样拥有i ...
- read design into DC memory
- matplotlib学习记录 四
# 绘制3月每天最高温和10月每天最高温散点图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...
- 20个必不可少的Python库也是基本的第三方库
个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都应该有它. Scrapy. ...
- (转)git常见错误
error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error: insufficient pe ...
- build_mem_type_table
该函数设置mem_types结构体数组,结构体定义如下: struct mem_type { unsigned int prot_pte; //二级页表属性 unsigned int prot ...
- stm32L011F3使用开发小记——开发坏境
今日,因工作需要,使用到了stm32L011F3芯片,此芯片基于CM0+内核,属于低功耗芯片 开发平台可以免费用于KEILMDK,keil公司用免费的许可证,网址:https://www2.keil. ...
- WIN 备份 重装
title: WIN 备份 重装 date: 2018-09-01 22:35:31 updated: tags: [windows,记录,折腾] description: keywords: com ...
- session属性的清除和非法登录
有的项目会将登录用户的资料存于session的一个属性中,这样方便获取一些数据使用,但是用户退出时需要将session的这个属性清除,一面造成一些不必要的麻烦,但是有些时候,在后台清除了这个属性,在拦 ...