gistfile1.txt
#include<iostream>
#include<math.h>
using namespace std;

#define ARRSIZE 200000

int Hash_Fun(int x, int times) { return (x % ARRSIZE + pow(times, 2)); }
void Cow()
{
int N, K;                  //读取奶牛数和特点数
cin >> N >> K;
int* Cows = new int[N];    //所有奶牛
int features = pow(2, K);  //特点值
int Sum = 0;
int maxRange = 0;
int* binary = new int[K];
for (int u = 0; u < K; u++)
binary[u] = 0;

static int record[ARRSIZE][33] = { 0 };

for (int i = 0; i < N; i++)//读取每一头奶牛的特点值
{
cin >> Cows[i];
Sum = (Sum + Cows[i]) % (features - 1);
bool flag = true;
for (int w = 0; w < K; w++)
{
binary[w] += Cows[i] % 2;
if (w > 0 && binary[w] != binary[w - 1])flag = false;
Cows[i] /= 2;
}
if (flag && (i + 1) > maxRange)maxRange = i + 1;
//Sum = (Sum + Cows[i]) % (features - 1);
int count = 0;
while (true)
{
flag = true;
for (int f = 1; f < K; f++)
if (binary[f] - record[Hash_Fun(Sum, count)][f] != binary[f - 1] - record[Hash_Fun(Sum, count)][f - 1]) { flag = false; break; }
if (flag)
{
record[Hash_Fun(Sum, count)][K + 1] = i - record[Hash_Fun(Sum, count)][K];
break;
}
else if (record[Hash_Fun(Sum, count)][K + 2] == 0)
{
record[Hash_Fun(Sum, count)][K + 2] = 1;
for (int f = 0; f < K; f++)
record[Hash_Fun(Sum, count)][f] = binary[f];
record[Hash_Fun(Sum, count)][K] = i;
break;
}
count ++;
}
}

for (int i = 0; i < ARRSIZE; i++)
{
if (record[i][K + 1] > maxRange)
maxRange = record[i][K + 1];
}

cout << maxRange;
}

int main()
{
Cow();
}