# “景驰科技杯”2018年华南理工大学程序设计竞赛 H 对称与反对称

Input:

Output:

Sample Input:
2 19260817
0 1
1 0
Sample Output:
0 0
0 0

$\left[ \begin{matrix} 0 & C1\\ C2 & 0\\ \end{matrix} \right]$

$\left[ \begin{matrix} 0 & x\\ x & 0\\ \end{matrix} \right]$

$\left[ \begin{matrix} 0 & y\\ -y & 0\\ \end{matrix} \right]$

$(x+y)\%m=C1\\ (x-y)\%m=C2$

$y=((C1-C2)÷2)\%m$

——百度百科

// 返回值为d(=gcd(a, b)),和ax+by=d中的x,y
long long extendgcd(long long a, long long b, long long &x, long long &y) {
if (a == 0 && b == 0) {
return -1;
}
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extendgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}


// 返回值为a关于mod的逆元
long long inverse(long long a, long long mod) {
long long x, y;
long long d = extendgcd(a, mod, x, y);
if (d == 1) {
return (x % mod + mod) % mod;
}
else {
return -1;
}
}


AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+5;
const double eps = 1e-5;
const double e = 2.718281828459;

int n;
ll m;
ll A[maxn][maxn];
ll C[maxn][maxn];

ll extendgcd(ll a, ll b, ll &x, ll &y) {
if (a == 0 && b == 0) {
return -1;
}
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extendgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}

ll inverse(ll a, ll mod) {
ll x, y;
ll d = extendgcd(a, mod, x, y);
if (d == 1) {
return (x % mod + mod) % mod;
}
else {
return -1;
}
}

void antisymmetric_matrix() {
ll inv = inverse(2, m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i < j) {
C[i][j] = (A[i][j] - A[j][i]) * inv % m;
C[j][i] = -C[i][j];
while (C[i][j] < 0) {
C[i][j] += m;
}
while (C[j][i] < 0) {
C[j][i] += m;
}
}
}
}
}

int main() {
ios::sync_with_stdio(0);
cin.tie(0);
while (cin >> n >> m) {
mem(A, 0);
mem(C, 0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> A[i][j];
}
}
antisymmetric_matrix();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cout << C[i][j];
if (j != n) {
cout << " ";
}
}
cout << endl;
}
}
return 0;
}