B. Integers Have Friends -双指针 同余 gcd树

Problem - B - Codeforces

B. Integers Have Friends

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that "every positive integer was one of his personal friends."

It turns out that positive integers can also be friends with each other! You are given an array aa of distinct positive integers.

Define a subarray ai,ai+1,…,ajai,ai+1,…,aj to be a friend group if and only if there exists an integer m≥2m≥2 such that aimodm=ai+1modm=…=ajmodmaimodm=ai+1modm=…=ajmodm, where xmodyxmody denotes the remainder when xx is divided by yy.

Your friend Gregor wants to know the size of the largest friend group in aa.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1041≤t≤2⋅104).

Each test case begins with a line containing the integer nn (1≤n≤2⋅1051≤n≤2⋅105), the size of the array aa.

The next line contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤10181≤ai≤1018), representing the contents of the array aa. It is guaranteed that all the numbers in aa are distinct.

It is guaranteed that the sum of nn over all test cases is less than 2⋅1052⋅105.

Output

Your output should consist of tt lines. Each line should consist of a single integer, the size of the largest friend group in aa.

Example

input

Copy

4
5
1 5 2 4 6
4
8 2 5 10
2
1000 2000
8
465 55 3 54 234 12 45 78

output

Copy

3
3
2
6

Note

In the first test case, the array is [1,5,2,4,6][1,5,2,4,6]. The largest friend group is [2,4,6][2,4,6], since all those numbers are congruent to 00 modulo 22, so m=2m=2.

In the second test case, the array is [8,2,5,10][8,2,5,10]. The largest friend group is [8,2,5][8,2,5], since all those numbers are congruent to 22 modulo 33, so m=3m=3.

In the third case, the largest friend group is [1000,2000][1000,2000]. There are clearly many possible values of mm that work.

========================================================================

同余的题目要转化 

ai 与 ai+1  ai+2同余,也就是说

ai= k1m + mod

ai+1 =k2m +mod

ai+2=k3m +mod

两两作差会发现,其差值都是m的倍数,也就是gcd一定是不为1的

这样问题就转化为了求出一个最长的连续差值区间,其区间gcd并不为1

考虑双指针做法,由于gcd的本质是对每个数的最大共有的因子,那么在数字越多时,gcd是呈现不升趋势的,言外之意就是,一个大区间的gcd若不为1,其内部子区间一定也不为1。这样我们可以设当前位置i为左端点时向右边最大扩展的位置为j  ,也就是区间 [I,J]gcd不为1,当i++时,[i+1,j]也是满足gcd不为1的,完全可以顺承过来,体现了双指针的思想。

至于区间GCD,用线段树即可

注意特判1

# include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

ll gcdtree[200000*4+10];
ll c[200000+10],a[200000+10];

void build(int root,int l,int r)
{
    if(l==r)
    {
        gcdtree[root]=c[l];
        return ;
    }

    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    gcdtree[root]=__gcd(gcdtree[root<<1],gcdtree[root<<1|1]);

}

ll getgcd(int root,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return gcdtree[root];

    }

    ll ans=0;

    int mid=(l+r)>>1;

    if(L<=mid)
        ans=__gcd(ans,getgcd(root<<1,l,mid,L,R));
    if(R>mid)
        ans=__gcd(ans,getgcd(root<<1|1,mid+1,r,L,R));
    return ans;

}
int main ()
{



   int t;
   cin>>t;

   while(t--)
   {
       int n;
       cin>>n;

       for(int i=1;i<=n;i++)
       {
           cin>>a[i];
       }
       if(n==1)
       {
           cout<<1<<endl;
           continue;
       }
       else
       {

           for(int i=1;i<n;i++)
           {
               c[i]=abs(a[i+1]-a[i]);
           }

           build(1,1,n-1);

           int j=1;

           int ans=0;
           for(int i=1;i<=n-1;i++)
           {
               while(j<=n-1&&getgcd(1,1,n-1,i,j)!=1)
                j++;

               ans=max(ans,j-i+1);
            }

            cout<<ans<<endl;
       }
   }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值