题目链接

题意

给定一个长度为 \(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. Linux 连接 Internet

    本文根据<鸟哥的Linux私房菜-服务器架设篇>第四章总结 Linux 连接 Internet 前的注意事项 想要连接 Internet 需要配置一组合法的 IP 参数,主要是 IP.Ne ...

  2. nuxt.js express模板项目服务器部署

    nuxt版本:0.10.6 技术栈:nuxt.js, express, pm2 部署环境:windows server 之前用nuxt.js 的express的模板项目在windows下用nginx进 ...

  3. JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿

    3388. [NOIP2013模拟]绿豆蛙的归宿 (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limi ...

  4. Java课堂作业

  5. Gym - 101981D Country Meow(模拟退火)

    题意 三维空间有\(n\)个点,找到另外一个点,离所有点的最大距离最小.求这个距离. 题解 \(1\).最小球覆盖,要找的点为球心. \(2\).模拟退火. 还是补一下模拟退火的介绍吧. 模拟退火有一 ...

  6. 动态规划:HDU1087-Super Jumping! Jumping! Jumping!(最大上升子序列和)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. Android开发——常见的内存泄漏以及解决方案(二)

    )Android2.3以后,SoftReference不再可靠.垃圾回收期更容易回收它,不再是内存不足时才回收软引用.那么缓存机制便失去了意义.Google官方建议使用LruCache作为缓存的集合类 ...

  8. poj2631 Roads in the North(求树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2941   Accepted: 144 ...

  9. Redis实现之RDB持久化(二)

    RDB文件结构 在Redis实现之RDB持久化(一)这一章中,我们介绍了Redis服务器保存和载入RDB文件的方法,在这一节,我们将对RDB文件本身进行介绍,并详细说明文件各个部分的结构和意义.图1- ...

  10. “帮你APP”团队冲刺8

    1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...