题解 矩阵

传送门

思路题

  • 对于一些在矩阵中进行行/列/斜线上加减的题目,(可能)有结论:
    对于任意一个 \(3*3\) 的矩阵,无论怎么处理,\(a_{1,1}-a_{1,2}-a_{2,1}+a_{2,3}+a_{3,2}-a_{3,3}\) 的值恒不变
    若没有斜线加减也有类似的结论

于是对于本题,只要能把前两行和前两列消干净,剩下的也就一定能消干净

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1010
#define ll long long
#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, m;
int mat[N][N];	
ll sum;

namespace task1{
	int dltx[N], dlty[N];
	ll x[N], y[N];
	void solve() {
		for (int i=2; i<=m; ++i) dltx[i]=dltx[i-1]+mat[1][i]-mat[1][i-1];
		for (int i=2; i<=n; ++i) dlty[i]=dlty[i-1]+mat[i][1]-mat[i-1][1];
		cout<<"dlty: "; for (int i=2; i<=n; ++i) cout<<dlty[i]<<' '; cout<<endl;
		ll k1, k2;
		for (int i=2; i<=m; ++i) k1+=dltx[i];
		for (int i=2; i<=n; ++i) k2+=dlty[i];
		k1*=n; k2*=m;
		sum-=n*m*(mat[1][1]);
		cout<<"sum: "<<sum<<endl;
		ll x1 = (sum-k2*mat[1][1])/(k1-k2);
		cout<<"x1: "<<x1<<endl;
		ll y1 = (sum-k1*mat[1][1])/(k2-k1);
		cout<<"y1: "<<y1<<endl;
		x[1]=x1; y[1]=y1;
		for (int i=2; i<=m; ++i) x[i]=x1+dltx[i];
		for (int i=2; i<=n; ++i) y[i]=y1+dlty[i];
		cout<<"x: "; for (int i=1; i<=m; ++i) cout<<x[i]<<' '; cout<<endl;
		cout<<"y: "; for (int i=1; i<=n; ++i) cout<<y[i]<<' '; cout<<endl;
		printf("%d\n", n+m);
		for (int i=1; i<=n; ++i) printf("1 %d %lld\n", i, y[i]);
		for (int i=1; i<=m; ++i) printf("2 %d %lld\n", i, x[i]);
	}
}

namespace task2{
	void solve() {
		cout<<n+m+1<<endl;
		printf("1 %d %d\n", n, -mat[n][1]);
		ll dlt=mat[n][1];
		for (int i=1; i<=m; ++i) mat[n][i]-=dlt;
		for (int i=1; i<=m; ++i) printf("2 %d %d\n", i, -mat[n][i]);
		for (int i=1; i<n; ++i) printf("1 %d %d\n", i, -mat[i][1]);
		exit(0);
	}
}

namespace task{
	int tot;
	struct opt{int a, b, c; inline void build(int x, int y, int z){a=x; b=y; c=z;}}op[6010];
	void op1(int x, int k) {
		op[++tot].build(1, x, k);
		for (int i=1; i<=m; ++i) mat[x][i]+=k;
	}
	void op2(int x, int k) {
		op[++tot].build(2, x, k);
		for (int i=1; i<=n; ++i) mat[i][x]+=k;
	}
	void op3(int x, int k) {
		op[++tot].build(3, x, k);
		for (int i=-x; i<=n&&i+x<=m; ++i) if (i>0&&i+x>0) mat[i][i+x]+=k;
	}
	void put() {for (int i=1; i<=n; ++i) {for (int j=1; j<=m; ++j) cout<<mat[i][j]<<' '; cout<<endl;}}
	void solve() {
		for (int i=m; i; --i) {
			op2(i, -mat[1][i]);
			op3(i-2, -mat[2][i]);
		}
		// put(); cout<<endl;
		for (int i=3; i<=n; ++i) {
			op1(i, -mat[i][2]);
			op3(1-i, -mat[i][1]);
		}
		// put(); cout<<endl;
		for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) if (mat[i][j]) {puts("-1"); exit(0);}
		printf("%lld\n", tot);
		for (int i=1; i<=tot; ++i) printf("%lld %lld %lld\n", op[i].a, op[i].b, op[i].c);
	}
}

signed main()
{
	freopen("c.in", "r", stdin);
	freopen("c.out", "w", stdout);

	n=read(); m=read();
	for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) sum+=(mat[i][j]=read());
	task::solve();
	
	return 0;
}
posted @ 2021-09-25 19:44  Administrator-09  阅读(2)  评论(0编辑  收藏  举报