题目链接

题意

给定一个长度为 \(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值的更多相关文章

  1. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

  2. codeforces 475D. CGCDSSQ

    D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes Given a sequence of int ...

  3. Codeforces 475D CGCDSSQ(分治)

    题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...

  4. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

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

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

  6. 区间加值,区间gcd, 牛客949H

    牛客小白月赛16H 小阳的贝壳 题目链接 题意 维护一个数组,支持以下操作: 1: 区间加值 2: 询问区间相邻数差的绝对值的最大值 3: 询问区间gcd 题解 设原数组为\(a\), 用线段树维护\ ...

  7. Codeforces 914D - Bash and a Tough Math Puzzle 线段树,区间GCD

    题意: 两个操作, 单点修改 询问一段区间是否能在至多一次修改后,使得区间$GCD$等于$X$ 题解: 正确思路; 线段树维护区间$GCD$,查询$GCD$的时候记录一共访问了多少个$GCD$不被X整 ...

  8. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  9. FZU2224 An exciting GCD problem 区间gcd预处理+树状数组

    分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...

随机推荐

  1. https://www.atlassian.com

    https://www.atlassian.com 解决:confluence 5.9.4 一次性恢复30个插件 - 简书 https://www.jianshu.com/p/c32d8aa739b8 ...

  2. 整合mybatis和spring时 Error creating bean with name 'sqlSessionFactory' defined in class path resource

    今天在整合mybatis和spring的时候出的错 报错如下 Exception in thread "main" org.springframework.beans.factor ...

  3. tp5依赖注入(自动实例化):解决了像类中的方法传对象的问题

    app\index\Demo1.php namespace app\index\controller; /* 容器与依赖注入的原理 ----------------------------- 1.任何 ...

  4. Python的while else

    python中有一个其独有的功能,while else. 它的作用是判断是循环是否被终止,如果没有被终止,那么就会执行else,反之则不会执行.还是用一段代码来解释吧 else被执行: count = ...

  5. Python 频繁请求问题: [Errno 104] Connection reset by peer

    Table of Contents 1. 记遇到的一个问题:[Errno 104] Connection reset by peer 记遇到的一个问题:[Errno 104] Connection r ...

  6. CQRS之旅——旅程3(订单和注册限界上下文)

    旅程3:订单和注册限界上下文 CQRS之旅的第一站 "寓言家和鳄鱼是一样的,只是名字不同" --约翰·劳森 描述: 订单和注册上下文有一部分职责在会议预订的过程中,在此上下文中,一 ...

  7. shell脚本杀掉指定进程下所有子进程(包括子进程的子进程)

    搜索了网上好像并没有杀掉指定进程下所有子进程(包括子进程的子进程)的脚本,自己琢磨写了一版,虽说比较简单,但希望分享大家,帮助需要的人 #!/bin/sh # 递归找到进程最底层子进程并杀除. mai ...

  8. 将FragmentManger事务添加到返回栈中

    FragmentManger事务添加或替换的 Fragment 后,这时点击 Back 键,程序并不会返回添加之前的状态. 我们可以使用 Transaction 对象的 addToBackStack( ...

  9. Python+Selenium中级篇之-封装一个自己的类-浏览器引擎类

    前一篇文章我们知道了,如何去封装几个简单的Selenium方法到我们自定义的类,这次我们编写一个类,叫浏览器引擎类,通过更改一个字符串的值,利用if语句去判断和控制启动那个浏览器.这里我们暂时,支持三 ...

  10. DFS排列组合问题

    这四个使用DFS来求解所有组合和排列的例子很有代表性,这里做一个总结: 1.不带重复元素的子集问题 public ArrayList<ArrayList<Integer>> s ...