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

题意:给你一个长度为n的序列a,给你一个数k,问你这个序列最大长度的子序列中各元素进行合取运算小于k。

思路:后缀和。

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=3e5+100;
int a[maxn];
int s[maxn];
int vis[maxn];
int main()
{
    int t;
    freopen("wifi.in", "r", stdin);
    cin>>t;
    while(t--)
    {
        int n,v;
        scanf("%d%d",&n,&v);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int ma=0;
        int l=1,r=1;
        memset(vis,0,sizeof(vis));
        int nowture=0;
        while(r<=n)
        {
            if(nowture>v)
            {
                int x=a[l++];
                int now=0;
                while(x)
                {
                    if(x&1) vis[now]--;
                    x>>=1;
                    now++;
                }
                nowture=0;
                for(int i=0;i<20;i++)
                {
                    if(vis[i]>0)
                    {
                        nowture+=(1<<i);
                    }
                }
                if(l>=r) r=l;
            }
            else
            {
                int x=a[r];
                int now=0;
                while(x)
                {
                    if(x&1) vis[now]++;
                    x>>=1;
                    now++;
                }
                nowture=0;
                for(int i=0;i<20;i++)
                {
                    if(vis[i]>0)
                    {
                        nowture+=(1<<i);
                    }
                }
                if(nowture<=v) ma=max(ma,r-l+1);
                r++;
            }
        }
        cout<<ma<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值