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 ...
随机推荐
- Python 潮流周刊第 13 期(2023-07-29)
查看全文: https://pythoncat.top/posts/2023-07-29-weekly 文章&教程 1.Jupyter Notebook 7 隆重发布 (英) 2.Python ...
- 好用工具: windows terminal
直接在微软商店搜索该软件即可,本文介绍无法使用微软商店的情况. 解题思路 当我们无法下载某个软件时,可直接去Github中寻找该项目,知道该软件资源并下载. 下载地址 https://github.c ...
- Vue: 在vscode中添加vue的代码片段
创建vue文件模板 打开vscode,文件–>首选项->用户代码片段,在弹出的搜索框中输入vue,回车 删除原内容,将如下内容粘贴 { "Print to console&quo ...
- 国标GB28181视频平台EasyGBS视频监控平台无法播放,抓包返回ICMP排查过程
国标GB28181视频平台EasyGBS是基于国标GB/T28181协议的行业内安防视频流媒体能力平台,可实现的视频功能包括:实时监控直播.录像.检索与回看.语音对讲.云存储.告警.平台级联等功能.国 ...
- 使用C#创建安装Windows服务程序(最全教程)
开发语言:C# 开发环境: Visual Studio 2022 微软官方文档:https://learn.microsoft.com/zh-cn/dotnet/framework/windows-s ...
- GPT-4 到底能帮你干点啥?
目录 1. 从哪儿聊起呢 2. 潮起潮退 3. 遇强则强,遇我则-- 3.1 玩法一:辅助技能提升 3.2 镜头背后的故事 3.3 玩法二:综合"技术选型" 3.4 镜头背后的故事 ...
- C++算法之旅、06 基础篇 | 第三章 图论
常用代码模板3--搜索与图论 - AcWing DFS 尽可能往深处搜,遇到叶子节点(无路可走)回溯,恢复现场继续走 数据结构:stack 空间:需要记住路径上的点,\(O(h)\). BFS使用空间 ...
- Iphone通过ssh进行访问
Iphone通过usb进行ssh访问文件系统 在公司里wifi很不给力,而我又想通过ssh访问我的iphone,进行一些权限访问,这时我们该 itunnel_mux_rev71这个工具可以帮我们的忙 ...
- Java 中的日期时间总结
前言 大家好,我是 god23bin,在日常开发中,我们经常需要处理日期和时间,日期和时间可以说是一定会用到的,现在总结下 Java 中日期与时间的基本概念与一些常用的用法. 基本概念 日期(年月日, ...
- c语言代码练习12
//计算1/1-1/2+1/3...-1/100的和#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { in ...