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 ...
随机推荐
- quarkus依赖注入之三:用注解选择注入bean
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<quarkus依赖注入> ...
- Log4j2的Maven依赖及其配置文件
Maven依赖 <!-- log4j 2依赖--> <dependency> <groupId>org.apache.logging.log4j</group ...
- 带你走进数仓大集群内幕丨详解关于作业hang及残留问题定位
本文分享自华为云社区<[带你走进DWS大集群内幕]大集群通信:作业hang.残留问题定位>,作者: 雨落天穹丶. 前言: 测试过程中,我们会遇到这样一种情况,我的作业都执行很久了,为啥还不 ...
- [FreeSWITCH]简单配置fifo呼入队列
拨号计划 <?xml version="1.0"?> <include> <context name="inboundcall"& ...
- CAP 7.2 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 7.2 版本正式版,我们在这个版本中主要致力于 Dashboard 对 k8s 服务发现的支持. 从 7.1 版本以来,我们发布了4个小版本,在这些版本中我们 ...
- 2023-08-20:用go语言写算法。给定一个由'W'、'A'、'S'、'D'四种字符组成的字符串,长度一定是4的倍数, 你可以把任意连续的一段子串,变成'W'、'A'、'S'、'D'组成的随意状
2023-08-20:用go语言写算法.给定一个由'W'.'A'.'S'.'D'四种字符组成的字符串,长度一定是4的倍数, 你可以把任意连续的一段子串,变成'W'.'A'.'S'.'D'组成的随意状态 ...
- 【pytorch】目标检测:一文搞懂如何利用kaggle训练yolov5模型
笔者的运行环境:python3.8+pytorch2.0.1+pycharm+kaggle.yolov5对python和pytorch版本是有要求的,python>=3.8,pytorch> ...
- 浅谈API安全的应用
理论基础 API它的全称是Application Programming Interface,也叫做应用程序接口,它定义了软件之间的数据交互方式.功能类型.随着互联网的普及和发展,API 从早期的 ...
- 最接地气的.NET微服务框架
前言: "人必有所执,方能有所成",从2018年底我就开始规划要写一个.NET微服务框架,5年了,今天终于正式发布了. 正文: Wing 致力于打造一个功能强大.最接地气的.NET ...
- CSP-J/S 初赛冲刺
CSP-J/S 初赛冲刺 对于咱们信奥选手来说,会做的题要坚决不丢分,不会做的题要学会尽量多拿分,这样你的竞赛之路才能一路亨通! Linux 基础操作 文件(文件夹)操作 列出文件:ls 列出隐藏文件 ...