Codeforces Round #736 (Div. 2). D - Integers Have Friends
原题:D - Integers Have Friends
题意:
给定一个数组,求一个最长子数组满足\(a_i \,\, mod \,\, m \,\, = \,\, a_{i + 1} \,\, mod \,\, m = ... \,\, = \,\, a_j \,\,mod\,\, m \,\,(m \geq 2)\)
求其最长长度。
根据同余定理:\(a≡b\,(mod \,\,m)\)等价于\(a - b\)可以被\(m\)整除。那么还有\(b[i] = a[i + 1] - a[i]\),所以说如果一个子数组同余于\(m\),那么这个子数组的差分数组一定可以被\(m\)整除,那么就说明这个子数组有一个最大公约数\(m\),所以我们可用线段树或者\(st\)表来维护一下区间的\(gcd\),那么对于求一个区间最大长度用双指针维护即可。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2E5 + 10, M = 20;
LL a[N], b[N];
int n;
LL f[N][M];
int l2g[N];
void init() {
for (int j = 0; j < M; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
if (!j) f[i][j] = b[i];
else f[i][j] = __gcd(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
}
}
LL query(int l, int r) {
int k = l2g[r - l + 1];
return __gcd(f[l][k], f[r - (1 << k) + 1][k]);
}
int main() {
int t; scanf("%d", &t);
l2g[2] = 1;
for (int i = 3; i <= N; i++) l2g[i] = l2g[i / 2] + 1;
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i <= n - 1; i++) b[i] = abs(a[i] - a[i + 1]);
if (n == 1) {
cout << 1 << endl;
continue;
}
init();
int res = 0;
int j = 1;
for (int i = 1; i <= n - 1; i++) {
while (j <= i && query(j, i) == 1) j++;
res = max(res, i - j + 2);
}
printf("%d\n", res);
}
return 0;
}
Codeforces Round #736 (Div. 2). D - Integers Have Friends的更多相关文章
- Codeforces Round #736 (Div. 2)
A,B,C就不说了,又被D题卡住了..... 感觉怎么说呢,就是题解中的三个提示都已经想到了,就是不知道该怎么解决.... D. Integers Have Friends 简述题意:题目要求你找一个 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)
Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...
- Codeforces Round #223 (Div. 2) A
A. Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
随机推荐
- Vue报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
错误原因,我猜测多半是版本问题 在router/index.js中添加如下代码 const originalPush = VueRouter.prototype.push VueRouter.prot ...
- golang trace view 视图详解
大家好,我是蓝胖子,在golang中可以使用go pprof的工具对golang程序进行性能分析,其中通过go trace 命令生成的trace view视图对于我们分析系统延迟十分有帮助,鉴于当前对 ...
- 集训后的一些wp
新生赛后 0x01 2048(jk出发) 修改跳转条件为nop,随便动一下直接打印flag.这个场景我幻想过很久了,一直没做到,这次参考wp做出来了知道怎么做了.学到! 0x02 four(dsact ...
- IDA函数特征识别自动签名
IDA函数特征识别自动签名 Vc6编译的有些无法识别一些库里面的函数 测试代码 #include <stdio.h> int main() { printf("123456\n& ...
- AI绘画:StableDiffusion制作AI赛博机车图保姆级教程
本教程收集于:AIGC从入门到精通教程汇总 如果你具备Stable Diffusion的基础知识,那制作AI赛博机车图会更为顺畅.即便没有这个基础,只要严格按照教程的步骤执行,也能成功制作出来. 本教 ...
- HTML视频背景(动态背景)
网页动态背景一般是用视频实现的,能增添网页的感染力,我觉得很好看,也不难,不妨学一下. 先加入下面一串代码: 1 <style> 2 video{ 3 height: 100%; 4 wi ...
- 4399 Flash游戏专用浏览器, 无需安装Flash插件
目前所有的主流浏览器都已经不再支持Flash了,即使有一些国内浏览器还支持flash,但只能安装国内特供版Flash Player. 但问题的关键在于,这个国内特供版跟 Adobe 海外发行的版本是两 ...
- Jenkins持续集成入门到精通(入门篇)
1. 什么是持续集成 持续集成(Continuous integration,简称CI)指的是频繁将代码集成到主干.它的目的,就是让产品可以快速迭代,同时保持高质量.核心措施,代码集成到主干之前,必须 ...
- 「codeforces - 1208F」Bits and Pieces
link. 考虑把原问题写成一个在 \(\left(\log_2 \max v \right) \times n\) 的矩阵里选出三列,我们首先预处理出 \(j \cap q\).具体,我们需要对于每 ...
- Solution -「HDU 3507」Print Article
Description Link. 给出 \(N\) 个单词,每个单词有个非负权值 \(C_{i}\),现要将它们分成连续的若干段,每段的代价为此段单词的权值和,还要加一个常数 \(M\),即 \(( ...