CF1548B Integers Have Friends
洛咕
题意:
- 给定 \(n\) 和一个长度为 \(n\) 的数组 \(a\),求一个最长的区间 \(\left[l,r\right]\),使得存在 \(m\geq 2\) 和 \(k\),对于所有 \(l\leq i\leq r,a_i\equiv k\pmod{m}\)(即区间内所有数对 \(m\) 取模余数相等),输出最长区间长度(区间长度定义为 \(r-l+1\))。
分析:
- 最大公约数也具有区间可并性。因此构建ST表,设\(f[i][j]\)表示\(i\) ~ \(i+\)\(2^j\)\(-1\)这段区间的最大公约数,预处理和维护都是模板,然后枚举长度可以用二分答案,时间复杂度约为\(nlog_2\)\(nlog_2\)\(n\).
#include<bits/stdc++.h>
#define ll long long
#define rg register
#define rep(i,j,k) for(int i=j;i<=k;++i)
using namespace std;
inline ll read(){
char ch=getchar();ll x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=1ll*x*10+ch-'0';ch=getchar();}
return x*f;
}
const int mod=100003;
const int N=2e5+5;
ll n,a[N],b[N],f[N][21],lg[N];
ll gcd(ll x,ll y){
if(!y)return x;
return gcd(y,x%y);
}
ll query(ll l,ll r){
ll x=lg[r-l+1];
return gcd(f[l][x],f[r-(1<<x)+1][x]);
}
bool check(ll mid){
for(int i=1;i+mid-1<=n;++i){
if(query(i,i+mid-1)>=2)return 1;
}
return 0;
}
int main(){
int T=read();
while(T--){
n=read();
for(int i=1;i<=n;++i)a[i]=read();
n--;
for(int i=1;i<=n;++i)b[i]=f[i][0]=abs(a[i]-a[i+1]);
for(int j=1;j<=20;++j){
for(int i=1;i+(1<<j)-1<=n;++i){
f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
lg[0]=-1;for(int i=1;i<=n;++i)lg[i]=lg[i>>1]+1;
int l=1,r=n,mid,ans=1;
while(l<=r){
mid=(l+r)>>1;
if(check(mid))l=mid+1,ans=mid+1;
else r=mid-1;
}
cout<<l<<endl;
}
return 0;
}
CF1548B Integers Have Friends的更多相关文章
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- LeetCode 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
随机推荐
- CRM管理系统界面
- JS的有关递归的知识点(数据无限级联的实现)
所用测试数据: 1 const data = [ 2 { 3 "area_id": 5, 4 "name": "广东省", 5 " ...
- 1405. 最长快乐字符串 (Medium)
问题描述 1405. 最长快乐字符串 (Medium) 如果字符串中不含有任何 'aaa', 'bbb' 或 'ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」. 给你三个整数 a, ...
- mysql库操作msyql
查看所有数据库 show databases; 查看当前使用的数据库 select database(); 使用数据库 use 数据库名; 创建数据库 create database 数据库名 cha ...
- python服务返回text与json
json.dumps(),loads()和jsonify()的区别 使用方法不同: dumps和loads方法,来自json模块,而json模块是python中的,可以直接导入: import jso ...
- 了解ASP(三) -- Cookie, Session, Application
ASP一共内建了7个对象,有Session.Application.Cookie.Response.Request.Server,这些对象都可以直接使用. 1. 什么是 Cookie? 1. cook ...
- 富文本编辑器第一次正常显示,第二次渲染失败 -----在使用laravel-admin 时
第二次显示 解决方法: 在每次获取富文本编辑器实例的时候,先删除一下,避免之前已经实例化造成的渲染失败
- unittestreport生成报告介绍
unittestreport主要有以下几个作用 接口用例HTML 测试报告生成. 测试用例失败重运行. 发送测试结果及报告到邮箱. 发送报告到钉钉群. 安装unittestreport: pip3 ...
- WPF-序列化
public class SerializeHelper { #region 二进制格式 /// <summary> /// Binary 序列化使用前需要标记类可序列化 /// < ...
- Docker私服(Registry)
Docker Registry安装 #拉取镜像 docker pull registry #创建文件夹 mkdir -p /var/my_registry #启动容器 docker run -d -- ...