Codeforces 475D CGCDSSQ 区间gcd值
题目链接
题意
给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) 满足\(\ (1\leq l\leq r\leq n)\) 且 \(gcd(a_l,a_{l+1},...,a_r)=x_i\)
思路
对于固定的右端点 \(i\),将左端点从右 (\(i\)) 向左 (\(1\)) 延伸,\(gcd\) 值是递减的,且变化次数不超过 \(logC\) (\(C\)为数列中最大值)
下面讲述两种方法,第一种效率高一些,而第二种也提供了一些新的见解。
法一:滚动数组 —— 更新分段信息
枚举右端点,将由左端点划分出的 \(gcd\) 值分段。每次用新加进来的 \(a_i\) 去与刚刚的若干段再取 \(gcd\) 并更新分段信息,更新的同时统计数目。
保存与更新分段信息 可用滚动数组实现,统计数目 则显然用map(要注意的一点是:需要用map<int, LL>
,因为数目可能会爆\(int\))。
法二:二分 + ST表 —— 找gcd值变化位置
参考自 hzwer.
如果说上一种做法是极大程度地利用了 上一次的信息,那么这一种做法就是抓住了 gcd值具有单调性 这个特点。
因此,确定分段位置可以直接采用二分查找,而如何快速地获取某一段的 \(gcd\) 值呢?就靠 \(ST\) 表大显身手了。
// 学到两点:
// 1. ST表适用的范围不仅局限于区间极值问题
// 2. 系统自带的log是真的慢...
Code
Ver. 1 : 171ms
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
int a[maxn];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct node { int x, p; };
map<int, LL> mp;
vector<node> v[2];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n; ++i) {
bool me = i & 1,
op = !me;
v[me].clear();
v[me].push_back({a[i], i});
int last = a[i];
for (auto nd : v[op]) {
int temp = gcd(nd.x, a[i]);
if (temp == last) v[me][v[me].size()-1].p = nd.p;
else v[me].push_back({temp, nd.p}), last = temp;
}
int now = i;
for (auto nd : v[me]) {
int pre = nd.p;
mp[nd.x] += now - pre + 1;
now = pre - 1;
}
}
int q, x;
scanf("%d", &q);
while (q--) {
scanf("%d", &x);
printf("%I64d\n", mp[x]);
}
return 0;
}
Ver. 2 : 296ms
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
int gcd[maxn][32], a[maxn], n, Log[maxn], bin[32];
map<int, LL> mp;
int Gcd(int a, int b) { return b ? Gcd(b, a%b) : a; }
void rmqInit() {
Log[0] = -1; bin[0] = 1;
for (int i = 1; i < 20; ++i) bin[i] = bin[i-1] << 1;
for (int i = 1; i <= n; ++i) Log[i] = Log[i>>1] + 1, gcd[i][0] = a[i];
for (int j = 1; bin[j] <= n; ++j) {
for (int i = 1; i + bin[j-1] - 1 <= n; ++i) {
gcd[i][j] = Gcd(gcd[i][j-1], gcd[i + bin[j-1]][j-1]);
}
}
}
int query(int l, int r) {
int k = Log[r-l+1];
return Gcd(gcd[l][k], gcd[r-bin[k]+1][k]);
}
int bi(int i, int l, int r, int x) {
while (r-l>1) {
int mid = l+r >> 1, val = query(i, mid);
if (val >= x) l = mid;
else r = mid - 1;
}
return query(i, r) == x ? r : l;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
rmqInit();
for (int i = 1; i <= n; ++i) {
int l = i;
while (true) {
if (l == n+1) break;
int val = query(i, l);
int r = bi(i, l, n, val);
mp[val] += r-l+1;
l = r+1;
}
}
int q, x;
scanf("%d", &q);
while (q--) {
scanf("%d", &x);
printf("%I64d\n", mp[x]);
}
return 0;
}
Codeforces 475D CGCDSSQ 区间gcd值的更多相关文章
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
- codeforces 475D. CGCDSSQ
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes Given a sequence of int ...
- Codeforces 475D CGCDSSQ(分治)
题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 区间加值,区间gcd, 牛客949H
牛客小白月赛16H 小阳的贝壳 题目链接 题意 维护一个数组,支持以下操作: 1: 区间加值 2: 询问区间相邻数差的绝对值的最大值 3: 询问区间gcd 题解 设原数组为\(a\), 用线段树维护\ ...
- Codeforces 914D - Bash and a Tough Math Puzzle 线段树,区间GCD
题意: 两个操作, 单点修改 询问一段区间是否能在至多一次修改后,使得区间$GCD$等于$X$ 题解: 正确思路; 线段树维护区间$GCD$,查询$GCD$的时候记录一共访问了多少个$GCD$不被X整 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- FZU2224 An exciting GCD problem 区间gcd预处理+树状数组
分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...
随机推荐
- nginx下根据指定路由重定向
前言: 最近在搭建vue后台,后端接口是PHP写的,线上构建好之后,需要请求其他域名下的接口,开发环境已经使用proxytable解决了接口问题,为了开发和生成的代码一致, 编译后的代码,放在ngin ...
- Vue 使用History记录上一页面的数据
UI Mvvm 前端数据流框架精讲 Vue数据双向绑定探究 面试问题:Vuejs如何实现双向绑定 数据双向绑定的探究和实现 需求 从列表页的第二页进入详情页,返回时列表页仍然显示在第二页: 从列表页的 ...
- Codeforces Round #459 (Div. 2):B. Radio Station
B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...
- 【PyTorch深度学习】学习笔记之PyTorch与深度学习
第1章 PyTorch与深度学习 深度学习的应用 接近人类水平的图像分类 接近人类水平的语音识别 机器翻译 自动驾驶汽车 Siri.Google语音和Alexa在最近几年更加准确 日本农民的黄瓜智能分 ...
- HDU 4628 Pieces(状态压缩+记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索 ...
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...
- 35、键盘布局的tableLayout备份
<TableLayout android:layout_width="wrap_content" android:layout_height="wrap_conte ...
- 【word ladder】cpp
题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...
- 用nc+简单bat/vbs脚本+winrar制作迷你远控后门
前言 某大佬某天和我聊起了nc,并且提到了nc正反向shell这个概念. 我对nc之前的了解程度仅局限于:可以侦听TCP/UDP端口,发起对应的连接. 真正的远控还没实践过,所以决定写个小后门试一试. ...
- 菜鸟之路——git学习及GitHub的使用
首先,感谢廖雪峰老师的git教程 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...