Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m × m of this matrix contains at least one zero.
Consider the following problem:
You are given two integers n and m. You have to construct an m-free square matrix of size n × n such that the number of 1's in this matrix is maximum possible. Print the maximum possible number of 1's in such matrix.
You don't have to solve this problem. Instead, you have to construct a few tests for it.
You will be given t numbers x1, x2, ..., xt. For every , find two integers ni and mi (ni ≥ mi) such that the answer for the aforementioned problem is exactly xi if we set n = ni and m = mi.
The first line contains one integer t (1 ≤ t ≤ 100) — the number of tests you have to construct.
Then t lines follow, i-th line containing one integer xi (0 ≤ xi ≤ 109).
Note that in hacks you have to set t = 1.
For each test you have to construct, output two positive numbers ni and mi (1 ≤ mi ≤ ni ≤ 109) such that the maximum number of 1's in a mi-free ni × ni matrix is exactly xi. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer - 1.
3
21
0
1
5 2
1 1
-1
【题意】
考虑这样一个题目“一个$$$n \times n$$$的0-1矩阵,要求每个$$$m \times m$$$的子矩阵都至少含有一个0,求这样的矩阵最多有多少个1”,现在已知1的个数$$$x$$$,求出任意一个满足条件的矩阵,并输出$$$m$$$和$$$n$$$,如果没有输出-1。
【思路】
每个0的点能使得它附近的$$$m \times m$$$的子矩阵都获得一个0,相当于每个0有一个覆盖范围,要让0最少就要让每个0的覆盖范围被充分利用
所以已知$$$n$$$和$$$m$$$就确定了1的个数$$$x$$$,满足下面的关系
$$$n^2-\lfloor\frac{n}{m}\rfloor^2 = x$$$
其实就是把x转化为两个数的方差,但唯一的限制是,小的数必须通过大的数做向下取整的除法得到。
令$$$t=\lfloor\frac{n}{m}\rfloor$$$,并将方程变形,得
$$$(n+t)*(n-t) = x$$$
遍历$$$x$$$的分解$$$x=a*b, (a>b)$$$,只要$$$a$$$和$$$b$$$同奇偶就能分解为平方差,得到$$$n=\frac{a+b}{2}$$$,$$$t=\frac{a-b}{2}$$$,只要存在一个$$$m$$$使得$$$t=\lfloor\frac{n}{m}\rfloor$$$,就找到答案了。这个的检验很容易,假设$$$m$$$就是$$$n/t$$$,只要$$$n/m = t$$$,就是满足的。
【代码】
1 #include<stdio.h> 2 #include<math.h> 3 4 5 int main() { 6 int t; 7 scanf("%d", &t); 8 9 while(t--){ 10 int x; 11 scanf("%d", &x); 12 if (x == 1)printf("-1\n"); 13 else if (x == 0)printf("1 1\n"); 14 else { 15 int flag = false; 16 //遍历 x 的约数 17 for (int i = 1; i*i < x;++i) { 18 if (x%i != 0)continue; 19 //检验是否同奇偶 20 if ((x / i + i) & 1 == 1)continue; 21 //检验是否对于n t是否存在一个符合的m 22 int n = (x / i + i) / 2,t = (x / i - i) / 2; 23 int m = n / t; 24 if (n / m != t)continue; 25 printf("%d %d\n", n, m); 26 flag = true; break; 27 } 28 if (flag == false)printf("-1\n"); 29 } 30 } 31 return 0; 32 }