题意:题目给了一个矩阵
A
的定义:
其中
M
是一个仅由 0 和 1 组成的矩阵
问,给定一个
分析: 这个题的关键就是从
A
矩阵中找出几个与
M
矩阵的等式关系。
我们用
用
用
则原定义变成了:
1.
可以发现的是,每个
Mij
是使
A
即
totM∗(2∗n−1)=totA
所以,如果
totA
不整除
2∗n−1
,答案为
0
。
所以会得到第二个等式:
RAi=totM+(n−1)∗RMi
所以,如果
RAi−totM
不整除
n−1
,答案也是
0
.
对于每个
3.
如果上面两个都满足,那么我们回到题目给的等式,
RMi
,
CMi
,
Aij
都是已知了,所以这样每个
Mij
都能求出来,而且是唯一的。求出来之后判断一下是否为
0
或
4. 还有一个特殊情况就是 n=1 的时候,直接特判一下即可。
以下是代码。
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
typedef pair<int, int>pii;
typedef pair<ll, ll>pll;
typedef pair<double, double>pdd;
const double eps = 1e-6;
const int MAXN = 1005;
const int MAXM = 100005;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const double FINF = 1000000000000000.0;
const ll MOD = 1000000007;
const double PI = acos(-1);
int f[MAXN][MAXN];
int row[MAXN], col[MAXN];
int main() {
memset(col, 0, sizeof(col));
memset(row, 0, sizeof(row));
int n, tot = 0; scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
scanf("%d", &f[i][j]);
row[i] += f[i][j];
col[j] += f[i][j];
tot += f[i][j];
}
}
if (n == 1) {
if (f[1][1] == 1 || f[1][1] == 0)goto ans_1;
else goto ans_0;
}
if (tot % (2 * n - 1) != 0)goto ans_0;
tot = tot / (2 * n - 1);
for (int i = 1; i <= n; ++i) {
if ((row[i] - tot) % (n - 1) || row[i] < tot)goto ans_0;
if ((col[i] - tot) % (n - 1) || col[i] < tot)goto ans_0;
row[i] = (row[i] - tot) / (n - 1);
col[i] = (col[i] - tot) / (n - 1);
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
int now = row[i] + col[j] - f[i][j];
if (now != 0 && now != 1)goto ans_0;
}
}
goto ans_1;
ans_0:
printf("0\n");
return 0;
ans_1:
printf("1\n");
return 0;
}