题解 矩阵

传送门

除了二进制分组什么都想了.jpg
基本复读zxy dalao的题解

考虑只有对角线为零怎么做
考虑对角线上的点行列编号相等
那么枚举二进制位,分别对此位 行为0列为1 和 行为1列为0 的做一次
这样是 \(O(2\log n)\)
考虑扩展到对角线下面那条斜线也是全零的情况
那么将列按奇偶分开,发现此时可以将相邻两行合为一行处理
若有一行全 1 的话在以后的每次操作中都包含这一行就可以了
这样总共的操作次数是 \(O(4\log n)\)

考虑一种神奇的 trick
发现含有恰好 6 个 1 的 13 位二进制数有 \(\binom{13}{6}>1500\)
那么用这些二进制数对需要处理的 \(\frac{n}{2}\) 个行和列做重标号
此时因为二进制数内包含的 1 的个数是定值
所以若行列标号不同一定存在至少一位满足行此位为 0 而列此位为 1
这样就可以做到 \(O(2\log n)=26\) 次操作了

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f
#define N 3010
#define pb push_back
#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, q;
int mp[N][N];

namespace force{
	int sta[N], top;
	void solve() {
		cout<<n<<endl;
		for (int i=1; i<=n; ++i) {
			top=0;
			for (int j=1; j<=n; ++j) if (mp[i][j]) sta[++top]=j;
			cout<<1<<' '<<top<<' '<<i<<' ';
			for (int j=1; j<=top; ++j) cout<<sta[j]<<' '; cout<<endl;
		}
	}
}

namespace task{
	int x[N], y[N], top;
	vector<int> fir[N], sec[N], id;
	void solve() {
		for (int s=0; s<(1<<13); ++s) if (__builtin_popcount(s)==6) id.pb(s);
		for (int i=1; i<=n; i+=2) y[i]=x[i]=x[i+1]=id[i>>1];
		for (int i=0; i<13; ++i) {
			++top;
			for (int j=1; j<=n; j+=2) if (!(y[j]&(1<<i))) sec[top].pb(j);
			for (int j=1; j<=n; ++j) if (x[j]&(1<<i)) fir[top].pb(j);
			if (!fir[top].size()||!sec[top].size()) fir[top].clear(), sec[top].clear(), --top;
		}
		for (int i=2; i<=n; i+=2) y[i]=x[i]=x[i+1]=id[i>>1];
		for (int i=0; i<13; ++i) {
			++top;
			for (int j=2; j<=n; j+=2) if (!(y[j]&(1<<i))) sec[top].pb(j);
			for (int j=2; j<=n; ++j) if (x[j]&(1<<i)) fir[top].pb(j);
			fir[top].pb(1);
			if (!fir[top].size()||!sec[top].size()) fir[top].clear(), sec[top].clear(), --top;
		}
		printf("%d\n", top);
		for (int i=1; i<=top; ++i) {
			printf("%d %d ", fir[i].size(), sec[i].size());
			for (auto it:fir[i]) printf("%d ", it);
			for (auto it:sec[i]) printf("%d ", it);
			printf("\n");
		}
	}
}

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

	n=read(); q=read();
	for (int i=1; i<=n; ++i) {
		for (int j=1; j+1<i; ++j) mp[i][j]=1;
		for (int j=i-1; j<=i; ++j) mp[i][j]=0;
		for (int j=i+1; j<=n; ++j) mp[i][j]=1;
	}
	// if (n==1) puts("0");
	// else if (n==2) {puts("1"); puts("1 1 1 2");}
	// else force::solve();
	task::solve();
	
	return 0;
}
posted @ 2022-05-28 06:55  Administrator-09  阅读(1)  评论(0编辑  收藏  举报