CodeForces - 1107D Compression(思维)

给定一个二进制矩阵,找到最大的正整数x,使得矩阵可以被压缩成x×x大小的矩阵,且压缩后的每个小矩阵内元素相同。题目要求解出这种压缩方式的最大x值,并提供了输入矩阵的压缩表示。解决方案包括查找所有相同子串并计算它们长度的最大公约数。

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

You are given a binary matrix A of size n×n. Let’s denote an x-compression of the given matrix as a matrix B of size nx×nx such that for every i∈[1,n],j∈[1,n] the condition A[i][j]=B[⌈i/x⌉][⌈j/x⌉] is met.

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

01
10
For the given matrix A, find maximum x such that an x-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 n (4≤n≤5200) — the number of rows and columns in the matrix A. It is guaranteed that n is divisible by 4.

Then the representation of matrix follows. Each of n next lines contains n4 one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from A to F). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number B is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.

Elements are not separated by whitespaces.

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

Examples
Input
8
E7
E7
E7
00
00
E7
E7
E7
Output
1
Input
4
7
F
F
F
Output
1
Note
The first example corresponds to the matrix:

11100111
11100111
11100111
00000000
00000000
11100111
11100111
11100111
It is easy to see that the answer on this example is 1.

题目大意:给你一个01矩阵,需要你把他分成边长为n的一堆小矩阵,要求这些小矩阵里面的元素都相等,求这个小矩阵的最长边长。

思路:遍历一整个矩阵,容易得知,小矩阵的边长n必定是任意一段元素都相同的01串的长度的因子。所以只需要找出所有连续的元素相同的子串,求他们的gcd即可。复杂度是n*n。
这个题好像也有前缀和的做法,看不太懂。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include<string>
#include<set>
#include<map>
#include<fstream>
#include<time.h>
#pragma warning(disable:6031)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const ll mode = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950;
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }

int n;
bool a[5211][5211];
int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }

void parse_char(int x, int y, char c) //把二进制矩阵存入a
{
	int num = -1;
	if (isdigit(c)) num = c - '0';
	else num = c - 'A' + 10;
	for (int i = 0; i < 4; ++i)
	{
		a[x][y + 3 - i] = num & 1;
		num >>= 1;
	}
}

int main()
{
	scanf("%d", &n);
	char buf[5211];
	for (int i = 0; i < n; i++) 
	{
		scanf("%s", buf);
		for (int j = 0; j < n / 4; ++j) 
			parse_char(i, j * 4, buf[j]);
	}

	int g = n;

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
		{
			int k = j;
			while (k < n && a[i][k] == a[i][j]) ++k;
			g = gcd(g, k - j);
			j = k - 1;
		}

	for (int j = 0; j < n; j++)
		for (int i = 0; i < n; i++)
		{
			int k = i;
			while (k < n && a[k][j] == a[i][j]) ++k;
			g = gcd(g, k - i);
			i = k - 1;
		}

	cout << g << endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值