首页 > 其他分享 >CodeForces 1917E Construct Matrix

CodeForces 1917E Construct Matrix

时间:2023-12-26 18:56:35浏览次数:25  
标签:typedef long CodeForces 1917E times 异或 Construct 矩形 define

洛谷传送门

CF 传送门

\(2 \nmid k\) 显然无解。

若 \(4 \mid k\),发现给一个全 \(2 \times 2\) 子矩形全部异或 \(1\) 不会对行异或和和列异或和造成影响。那么我们找到 \(\frac{k}{4}\) 个全 \(0\) 的 \(2 \times 2\) 子矩形填 \(1\) 即可。

否则若 \(k = 2\) 或 \(k = n^2 - 2\),除 \(n = 2\) 外无解。

否则我们有一个 \(k = 6\) 的构造,即令 \(a_{1, 1} = a_{1, 2} = a_{2, 1} = a_{2, 3} = a_{3, 2} = a_{3, 3} = 1\)。

若 \(k\) 更大,把左上角的 \(4 \times 4\) 矩形抠掉后再找 \(2 \times 2\) 全 \(0\) 矩形,全部填 \(1\)。

若 \(k\) 还有剩余,在左上角 \(4 \times 4\) 矩形中找和为 \(1\) 的 \(2 \times 2\) 子矩形,全部异或上 \(1\),可以使得这个子矩形的和变为 \(3\)。重复这样找直到 \(k\) 被消耗尽即可。容易发现一定能找到这样的子矩形。

code
// Problem: E. Construct Matrix
// Contest: Codeforces - Codeforces Round 917 (Div. 2)
// URL: https://codeforces.com/contest/1917/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 1010;

int n, m, a[maxn][maxn];

void solve() {
	scanf("%d%d", &n, &m);
	if (m & 1) {
		puts("No");
		return;
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			a[i][j] = 0;
		}
	}
	if (m % 4 == 0) {
		for (int i = 1; i < n && m; i += 2) {
			for (int j = 1; j < n && m; j += 2) {
				a[i][j] = a[i + 1][j] = a[i][j + 1] = a[i + 1][j + 1] = 1;
				m -= 4;
			}
		}
	} else {
		if (n == 2) {
			puts("Yes\n1 0\n0 1");
			return;
		}
		if (m == 2 || m == n * n - 2) {
			puts("No");
			return;
		}
		a[1][1] = a[1][2] = a[2][1] = a[2][3] = a[3][2] = a[3][3] = 1;
		m -= 6;
		for (int i = 1; i < n && m; i += 2) {
			for (int j = 1; j < n && m; j += 2) {
				if (i < 4 && j < 4) {
					continue;
				}
				a[i][j] = a[i + 1][j] = a[i][j + 1] = a[i + 1][j + 1] = 1;
				m -= 4;
			}
		}
		while (m) {
			bool fl = 1;
			for (int i = 1; i < 4 && fl; i += 2) {
				for (int j = 1; j < 4 && fl; j += 2) {
					if (a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 1) {
						a[i][j] ^= 1;
						a[i + 1][j] ^= 1;
						a[i][j + 1] ^= 1;
						a[i + 1][j + 1] ^= 1;
						m -= 2;
						fl = 0;
					}
				}
			}
		}
	}
	puts("Yes");
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			printf("%d ", a[i][j]);
		}
		putchar('\n');
	}
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:typedef,long,CodeForces,1917E,times,异或,Construct,矩形,define
From: https://www.cnblogs.com/zltzlt-blog/p/17929063.html

相关文章

  • constructor中的super
    1.ES6要求,子类的构造函数必须执行一次super函数。这是必须的,否则JavaScript引擎会报错。在执行super函数时,其实就是在创建子类的this,然后将父类的实例和方法放置在这个this对象中,子类在调用super之前是没有this的,所有的this操作都要在super()关键字后执行。由于super指向父类的......
  • CodeForces 1917F Construct Tree
    洛谷传送门CF传送门考虑形式化地描述这个问题。先把\(l\)排序。然后相当于是否存在一个\(\{1,2,\ldots,n\}\)的子集\(S\),使得:\(\sum\limits_{i\inS}l_i=d\)。\(\existsT\subseteqS,\max\limits_{i\notinS}l_i\le\min(\sum\limits_{i\inT}l_i,\sum......
  • Codeforces Round 915 (div2) E
    E.TreeQueries[题目链接](https://codeforces.com/contest/1904/problem/EProblem-E-Codeforces)题意概括:给定一棵大小为\(n\)的树,回答如下询问,询问之间相互独立:给定一个点\(x\)与\(k\)个点\(a_i\),求出从\(x\)出发不经过任何一个\(a_i\)的最长简单路径长度......
  • codeforces刷题(1100):1917B_div2
    模板B、EraseFirstorSecondLetter跳转原题点击此:该题地址1、题目大意  给你一个字符串,可以执行任意次以下操作,生成最终的字符串(不可为空),问你能生成的不重复字符串数为多少。操作一:删除字符串第一个字符;操作二:删除字符串第二个字符。2、题目解析  发现,操作一:即选......
  • Codeforces 1909G - Pumping Lemma
    这个题思考角度很多,做法也很多。这里介绍一种@asmend和我讲的做法。设\(d=m-n\),那么我们枚举\(|x|=i,|y|=j\),设\(s,t\)的LCP长为\(l_1\),LCS长为\(l_2\),那么可以得到这组\((i,j)\)合法的充要条件是:\(i\lel_1\)\(m-i-j-d\lel_2\)。\(d\bmodj=0\)。\(t[i,i+d-1......
  • Codeforces1917F - Construct Tree
    Codeforces1917F-ConstructTreeProblems给一个长度为\(n\)的序列\(l\)和\(d\)。要求判断是否可以构造出一颗节点数为\(n+1\)的树,满足\(l\)的每一个元素唯一对应为一条边的长度,并使整棵树的直径长度恰好为\(d\)。Solution不妨令\(l_1\lel_2\le\cdots\lel_......
  • CodeForces 1906K Deck-Building Game
    洛谷传送门CF传送门UNR#2黎明前的巧克力。枚举两个人选的卡的并集\(S\),那么当\(\bigoplus\limits_{i\inS}a_i=0\)时\(S\)有贡献\(2^{|S|}\)。考虑将\(2^{|S|}\)分摊到每个元素上,也就是每个元素有\(2\)的贡献,然后把这些贡献乘起来。所以题目其实是想让我们算......
  • Codeforces1917E - Construct Matrix
    Codeforces1917E-ConstructMatrix首先考虑因为\(n\)为偶数,所以\(k\)为奇数时不可能满足条件。其次,如果\(4|k\),那么实际上在矩阵中一直放\(2\times2\)的全为\(1\)的矩阵就可以了。随后,如果\(k\equiv2\mod4\),那么可以证明如果\(k\ne2\landk\nen^2-2\),......
  • Codeforces Round 917 (Div. 2)
    基本情况A题秒了,B题卡了一年。B.EraseFirstorSecondLetterProblem-B-Codeforces卡题分析两方面原因没有变通,一开始的思路是公式算出总字串数再想办法找重复的减掉,但搞了一个小时都不可行,应该早点换成正着来找的思路。没有更深入的分析样例。最后虽然出来了,......
  • codeforces刷题(1100):1907C_div3
    C、RemovalofUnattractivePairs跳转原题点击此:该题地址1、题目大意  给定一个字符串,可以删除相邻的两个不相等的字符。问你删除后能得到最小的字符串长度为多少。2、题目解析  因为只要两个不相等的字符相邻就能消除,所以只需要找到数量最多的字符,只要它的数量比其它字......