二维前缀和 codeforce div2 D. Compression

D. Compression

题意

给一个n
有n行16进制的数
刚好变成nn 的01矩阵
将n
n矩阵划分成 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值