当前位置:网站首页>Integers have friends interval GCD + double pointer
Integers have friends interval GCD + double pointer
2022-04-23 06:21:00 【Bzdhxs_ nt】
Ideas
For two adjacent numbers a b
Interval module p p p congruence a % p = k 1 ∗ x + c , b % p = k 2 ∗ x + c a\%p = k1*x+c , b\%p = k2*x+c a%p=k1∗x+c,b%p=k2∗x+c Introduction ( a − b ) % p = k ∗ x (a-b) \% p = k*x (a−b)%p=k∗x
Consider the differential array , If there is an interval in the difference array [ l , r ] , g c d > 1 [l,r] ,gcd >1 [l,r],gcd>1 . In the original array interval [ l − 1 , r ] [l-1,r] [l−1,r] Modulo identical number x x x congruence
Maintenance interval g c d gcd gcd Sure Segment tree and can be maintained dynamically
There is no need to modify S T ST ST Tables can also maintain intervals g c d gcd gcd
How to enumerate the answers ?
Violent enumeration is not desirable , Because of the interval g c d gcd gcd Monotonicity , It can be divided into two parts It's fine too
Enumerate right endpoints r r r, Then move the left endpoint l l l
Code
#define int long long
#define endl "\n"
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a; i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define dbg() cout <<"0k!"<< endl;
typedef long long ll;
int pri[16] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
const int inf = 0x3f3f3f3f;
const int INF = ~0ULL;
const int N = 1e6+10;
int a[200005],b[200005];
// struct node{
// int l,r;
// int dat;
// #define ls i<<1
// #define rs i<<1|1
// }t[200005<<2];
// void build(int i,int l,int r){
// t[i].l = l,t[i].r = r;
// if(l==r){
// t[i].dat = b[l];
// return;
// }
// int mid = (l+r) >> 1;
// build(ls,l,mid);
// build(rs,mid+1,r);
// t[i].dat = __gcd(t[ls].dat,t[rs].dat);
// }
// int ask(int i,int l,int r){
// if(l <= t[i].l && r >= t[i].r){
// return t[i].dat;
// }
// int res = 0;
// int mid = (t[i].l+t[i].r)>>1;
// if(l <= mid) res = __gcd(res,ask(ls,l,r));
// if(r >mid ) res = __gcd(res,ask(rs,l,r));
// return abs(res);
// }
int f[200005][21];
int n;
void ST(){
// No initialization
forr(i,1,n) f[i][0] = b[i];
forr(i,1,20)for(int j = 1;j+(1<<i)-1 <= n;j++){
f[j][i] = __gcd(f[j][i-1],f[j+(1<<(i-1))][i-1]);
}
}
void solve(){
cin>>n;
forr(i,1,n) cin>> a[i],b[i] = abs(a[i]-a[i-1]);
ST();
if(n==1){
cout << 1 << endl;
return;
}
//build(1,1,n);
int l = 2; // Minutiae Because the differential array b[1] Same as the original array So we have to go from l=2 Start enumeration , Because sometimes b[1] It may lead to answers within the range of answers provided +1, Examples 2
int res = 1;
for(int r = 2;r <= n;r++){
if(b[r]==1) continue;
while(l < r){
int len =__lg(r-l+1);
if(__gcd(f[r-(1<<len)+1][len],f[l][len]) == 1) l++;
else break;
}
res = max(res,r-l+2);
}
cout << res << endl;
}
signed main()
{
_orz;
int t;cin>>t;
while(t--) solve();
return 0;
}
版权声明
本文为[Bzdhxs_ nt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617219217.html
边栏推荐
- Detection technology and principle
- Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
- 去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
- Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
- String notes
- Custom exception class
- You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
- Event listener
- MySQL occasional Caton
- The bottom implementation principle of thread - static agent mode
猜你喜欢

Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations

Gaussian processes of sklearn

The bottom implementation principle of thread - static agent mode

Linear algebra Chapter 1 - determinant

PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比

Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()

RPC must know and know

Fundamentals of digital image processing (Gonzalez) I

Linear algebra Chapter 2 - matrices and their operations

Anaconda
随机推荐
Collection and map thread safety problem solving
3. Continuous integer
Implementation of displaying database pictures to browser tables based on thymeleaf
Collections multiple parameter sorting
EditorConfig
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
Graphic numpy array matrix
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
The bottom implementation principle of thread - static agent mode
PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
JDBC operation transaction
数字图像处理基础(冈萨雷斯)一
Consistent hash algorithm used for redis cache load balancing
String notes
Illustrate the significance of hashcode
Installation and usage skills of idea
线性代数第一章-行列式
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
Best practices for MySQL storage time