题解 矩阵

发布时间:2022-07-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了题解 矩阵脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

传送门

思路题

  • 对于一些在矩阵中进行行/列/斜线上加减的题目,(可能)有结论: 对于任意一个 (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("%dn", n+m);
		for (int i=1; i<=n; ++i) printf("1 %d %lldn", i, y[i]);
		for (int i=1; i<=m; ++i) printf("2 %d %lldn", i, x[i]);
	}
}

namespace task2{
	void solve() {
		cout<<n+m+1<<endl;
		printf("1 %d %dn", 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 %dn", i, -mat[n][i]);
		for (int i=1; i<n; ++i) printf("1 %d %dn", 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&amp;&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("%lldn", tot);
		for (int i=1; i<=tot; ++i) printf("%lld %lld %lldn", 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;
}

脚本宝典总结

以上是脚本宝典为你收集整理的题解 矩阵全部内容,希望文章能够帮你解决题解 矩阵所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。