Compression

给定一个二进制矩阵,找到最大的整数x,使得矩阵能被压缩成x*x大小的矩阵,且每个小块内全为0或全为1。通过前缀和计算并枚举矩阵的因子来确定最大x。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()是否可行。

枚举n的因子x不会超过2*sqrt(n)个

#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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值