尺取+二進制 WiFi Password

Just days before the JCPC, your internet service went down. You decided to continue your training at the ACM club at your university. Sadly, you discovered that they have changed the WiFi password. On the router, the following question was mentioned, the answer is the WiFi password padded with zeros as needed.

A subarray [l, r] of an array A is defined as a sequence of consecutive elements Al, Al + 1, ..., Ar, the length of such subarray is r - l + 1. The bitwise OR of the subarray is defined as: Al OR Al + 1 OR ... OR Ar, where OR is the bitwise OR operation (check the notes for details).

Given an array A of n positive integers and an integer v, find the maximum length of a subarray such that the bitwise OR of its elements is less than or equal to v.

Input

The first line contains an integer T (1 ≤ T ≤ 128), where T is the number of test cases.

The first line of each test case contains two space-separated integers n and v (1 ≤ n ≤ 105) (1 ≤ v ≤ 3 × 105).

The second line contains n space-separated integers A1, A2, ..., An(1 ≤ Ai ≤ 2 × 105), the elements of the array.

The sum of n overall test cases does not exceed 106.

Output

For each test case, if no subarray meets the requirement, print 0. Otherwise, print the maximum length of a subarray that meets the requirement.

Example

Input

3
5 8
1 4 5 3 1
5 10
8 2 6 1 10
4 2
9 4 5 8

Output

5
3
0

Note

To get the value of x OR y, consider both numbers in binary (padded with zeros to make their lengths equal), apply the OR operation on the corresponding bits, and return the result into decimal form. For example, the result of 10 OR 17 = 01010 OR 10001 = 11011 = 27.

題目大意:從一個數組裏找一段區間,把區間裏的所有值or起來計算結果,求結果不超過v的最長區間。

 右端點每次往右移
大於v時更新左端點
更新時每次把數組上的對應數減一
數組用來記錄二進制數位上的值

 記錄第i位上有幾個
然後去掉一個值的時候 把對應數位減一


 #include <cstdio>
#include<string.h>
int f[50];

int jisuan()
{
    int ans=0;

    int p=1;
    for (int i=0;i<=30;i++)
    {
        if (f[i]) ans+=p;
          p*=2;
    }

    return ans;
}

int main() {
    int l,r,n,v,i,u,t,o,a[100005];
    
    scanf("%d",&t);
    
    for (u=0;u<t;u++)
    {
         scanf("%d%d",&n,&v);
         for (i=0;i<n;i++) scanf("%d",&a[i]);
        
         l=0;r=0;o=0;
         memset(f,0,sizeof(f));
         while (r<n)
         {
             while (jisuan()<=v)
             {
                if ((r-l)>o) o=r-l;
                int k=0,q=a[r];
                while (q>0)
                {
                    f[k]+=q%2;
                    k++;
                    q/=2;
                } 
                r++;
                if (r>n) break;
            }
             while (jisuan()>v)
             {
                 int k=0,q=a[l];
                 while (q>0)
                {
                    f[k]-=q%2;
                    k++;
                    q/=2;
                }
                l++;
                if (l>=r&&r>=n) break;
             }
         }
         printf("%d\n",o);
      }
    
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章