题意
给一个n
有n行16进制的数
刚好变成nn 的01矩阵
将nn矩阵划分成 n/x个x*x的矩阵(此矩阵全为1或0)
问 最大的x是多少
思路
前缀和
暴力判断
AC代码
#include <bits/stdc++.h>
#define endl "\n"
#define INF 0x3f3f3f3f3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
const double EI = exp(1.0);
const int N = 5550;
const int maxn = 205;
const double eps = 1e-8;
int icase = 0;
int len = 0;
int a[N][N];
int pre[N][N];
int input(char ch)
{
if (ch <= '9' && ch >= '0')return ch - '0';
else
return ch - 'A' + 10;
}
int n;
bool check(int x)
{
int num1 = 0, num2 = 0;
for (int i = x; i <= n; i += x)
{
for (int j = x; j <= n; j += x)
{
if (pre[i][j] - pre[i][j - x] - pre[i - x][j] + pre[i - x][j - x] == 0)
num1++;
else if (pre[i][j] - pre[i][j - x] - pre[i - x][j] + pre[i - x][j - x] == x * x)
num2++;
}
}
x = n / x;
if (num1 + num2 == x*x)
return 1;
else
return 0;
}
void solve()
{
memset(a, 0, sizeof(a));
memset(pre, 0, sizeof(pre));
cin >> n;
for (int i = 1; i <= n; i++)
{
string str;
cin >> str;
for (int j = 1; j <= n/4; j ++)
{
int now = input(str[j - 1]);
if (now == 0)continue;
for(int k=4;k>=1;k--)
{
a[i][(j - 1) * 4 + k] = now % 2;
now /= 2;
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1]+a[i][j];
}
}
for (int i = n; i >= 1; i--)
{
if (n % i != 0)continue;
if (check(i))
{
cout << i << endl;
return;
}
}
}
int main()
{
std::ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//int t; cin >> t; while (t--)
solve();
return 0;
}