欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Compression

程序员文章站 2022-03-01 17:28:08
...

http://codeforces.com/contest/1107/problem/D

You are given a binary matrix AA of size n×nn×n . Let's denote an xx -compression of the given matrix as a matrix BB of size nx×nxnx×nx such that for every i∈[1,n],j∈[1,n]i∈[1,n],j∈[1,n] the condition A[i][j]=B[⌈ix⌉][⌈jx⌉]A[i][j]=B[⌈ix⌉][⌈jx⌉] is met.

Obviously, xx -compression is possible only if xx divides nn , but this condition is not enough. For example, the following matrix of size 2×22×2 does not have any 22 -compression:

0101

1010

For the given matrix AA , find maximum xx such that an xx -compression of this matrix is possible.

Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.

Input

The first line contains one number nn (4≤n≤52004≤n≤5200 ) — the number of rows and columns in the matrix AA . It is guaranteed that nn is divisible by 44 .

Then the representation of matrix follows. Each of nn next lines contains n4n4 one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 00 to 99 or as uppercase Latin letters from AA to FF ). Binary representation of each of these numbers denotes next 44 elements of the matrix in the corresponding row. For example, if the number BB is given, then the corresponding elements are 1011, and if the number is 55 , then the corresponding elements are 0101.

Elements are not separated by whitespaces.

Output

Print one number: maximum xx such that an xx -compression of the given matrix is possible.

Examples

Input

Copy

8
E7
E7
E7
00
00
E7
E7
E7

Output

Copy

1

Input

Copy

4
7
F
F
F

Output

Copy

1

Note

The first example corresponds to the matrix:

1110011111100111

1110011111100111

1110011111100111

0000000000000000

0000000000000000

1110011111100111

1110011111100111

1110011111100111

It is easy to see that the answer on this example is 11 .

 

题意:将01方阵分为x*x块,每块全0或者全1,使x最大。

解法:用二位前缀和可以o(1)得到任意子矩阵的和,全0为0,全1为x*x。然后从大到小枚举x,check()是否可行。

#include<bits/stdc++.h>
using namespace std;

int n;
bool a[5210][5210];
int sum[5210][5210];
char str[1500];

void input()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",str+1);
		for(int j=1;j<=n/4;j++)
		{
			int num= (str[j]<'A'?str[j]-'0':str[j]-'A'+10);
			for(int k=3;k>=0;k--)a[i][j*4-k]= (num&(1<<k)?1:0);
		}
	}
}
void init()
{
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
		sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
}
bool check(int x)
{
	for(int i=x;i<=n;i+=x)
	{
		for(int j=x;j<=n;j+=x)
		{
			int x2=i,y2=j;
			int x1=i-x+1,y1=j-x+1;
			int s=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
			if(s!=0&&s!=x*x)return false;
		}
	}
	cout<<x<<endl;
	return true;
}

int main()
{
////freopen("input.in","r",stdin);
	input();
	init();
	for(int x=n;x>=1;x--)if(n%x==0&&check(x))break;
	return 0;
}