Question: Give you a two-dimensional matrix A, and ask for the maximum value of x when the conditions are met ;
be sure to note that the two-dimensional matrix B is a compressed matrix. Why do you say this: For
example:
8 8 matrix, then when x =2:
1. When B[1][1], i=1 and j=1*2, so the value of i is 1, and the value of j is 1, so:
2. When When B[1][2], the value range of i is 1,2, and the value range of j is 3,4. So: it
can be found that a B[][] element corresponds to a small matrix in A; and because in B There are only two values of 0 and 1, then it means that the internal values of the small matrix in A corresponding to the elements in B are either all 0 or all 1, so this problem can be transformed into using prefix and then block Just cite; the
time complexity is o(n^2);
AC code:
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int sum[5205][5205];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>s;
for(int j=0;j<s.length();j++){
if(s[j]<='9'&&s[j]>='0'){
int t=s[j]-'0';
for(int k=j*4+1,y=3;y>=0;k++,y--){
//位移来判断是否为1 这里是顺着来的 就是注意下标
if((t>>y)&1)sum[i][k]=1;
else sum[i][k]=0;
}
}else if(s[j]<='F'&&s[j]>='A'){
int t=s[j]-'A'+10;
for(int k=j*4+1,y=3;y>=0;k++,y--){
if((t>>y)&1)sum[i][k]=1;
else sum[i][k]=0;
}
}
}
}
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];
}
}
int ans=0;
for(int i=n;i>=1;i--){
if(n%i==0){
//整除的原因是由于B[i/x][j/x],分段的结果 比如:x==2时,那么i就只能取值1,2,j只能取值1,2;如果x==3,那么i取值1,2,3,j取值为1,2,3
int num1=0;
int num2=0;
for(int j=i;j<=n;j+=i){
//遍历每一块的前缀和
for(int k=i;k<=n;k+=i){
if(sum[j][k]-sum[j-i][k]-sum[j][k-i]+sum[j-i][k-i]==0)num1++;
else if(sum[j][k]-sum[j-i][k]-sum[j][k-i]+sum[j-i][k-i]==i*i)num2++;
}
}
int tt=n/i;//一个分为了 这么多块
if(num1+num2==tt*tt){
ans=i;break;
}
}
}
printf("%d\n",ans);
return 0;
}