原题: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的更多相关文章

  1. Codeforces Round #736 (Div. 2)

    A,B,C就不说了,又被D题卡住了..... 感觉怎么说呢,就是题解中的三个提示都已经想到了,就是不知道该怎么解决.... D. Integers Have Friends 简述题意:题目要求你找一个 ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  8. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  9. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  10. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

随机推荐

  1. HTML超文本标记语言3

    三.HTML表单标签 1.form标签 <form> form标签 name=表单名称,action=表单提交的地址,method=表单提交方式:get/post get/post详解: ...

  2. python教程 入门学习笔记 第1天 初识python python语言环境安装 python编写器

    初识python 一.python语言简介: 1.起源:1989年由荷兰的前谷歌程序员吉多.范罗苏姆(龟叔)创造,python的命名来源于英国电视喜剧Monty Python's Flying Cir ...

  3. don't be shy to use reshape

    don't be shy to use reshape

  4. quarkus依赖注入之十三:其他重要知识点大串讲(终篇)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<quarkus依赖注入> ...

  5. 【故障公告】多年的故障老朋友又来了:数据库服务器 CPU 100%

    数据库服务器 CPU 100% 问题几乎每年都要来几次,从来都不事先打一声招呼,今年的第2次在我们正忙着会员救园的时候来了. 今天 13:35 首先收到我们自己的异常告警通知: Execution T ...

  6. Unity UGUI的Button组件的介绍及使用

    UGUI的Button(按钮)组件的介绍及使用 1. 什么是UGUI的Button组件? UGUI(Unity GUI)是Unity引擎中的一套用户界面系统,Button(按钮)是其中的一个常用组件. ...

  7. 谈一谈电商API接口

    随着电商行业的快速发展,越来越多的企业开始意识到并利用API接口来提升其电商平台的功能和效率.但是,对于普通用户来说,对API接口可能还不太了解.那么,什么是API接口,特别是电商API接口呢?本文将 ...

  8. HDFS核心概念与架构

    HDFS简介 HDFS是Hadoop项目的核心子项目,在大数据开发中通过分布式计算对海量数据进行存储与管理,它基于流数据模式访问和处理超大文件的需求而开发,可以运行在廉价的商用服务器上,为海量数据提供 ...

  9. 前端三件套系例之CSS——CSS3基础样式

    文章目录 1.宽和高 案例 2.字体属性 2-1 文字字体 2-2 字体大小 2-3 字重(粗细) 2-4 文本颜色 2-5 总结 2-6 案例 文字属性 3-1 文字对齐 3-2 文字装饰 3-3 ...

  10. Oracle11g安装教程(带安装包)

    找了半天没在官网上找到Oracle11g的安装包下载,又找了半天,终于在网上的一个教程里找到安装包的网盘链接.现在在这记一下防止以后重新找麻烦. 网盘链接 百度云盘链接:[https://pan.ba ...