CF1956C Nene's Magical Matrix 题解

CF1956C Nene's Magical Matrix

被这题送走了,纪念一下。

巧妙的构造题,考虑比较方便处理的方案,假设我们从左上角的顶点开始涂,每次涂一个 \(1,2,3\dots n\) 的排列。其余方案可以通过这种方案交换数字顺序得到,所以只考虑这种方案。

考虑分析总和最大时矩阵的性质。显然,最内圈最大为 \(1\),最外圈最大为 \(n\)。最后的矩阵必然是这个形式:

\[\begin{bmatrix}1&2&\dots&n\\2&2&\dots&n\\\dots&\dots&\dots&n\\n&n&n&n&\\\end{bmatrix} \]

接下来,我们考虑构造一种方案得到这个矩阵。我们只需要从外圈到内圈,纵横交错着涂。这样,每一圈不符合要求的数字就会被覆盖,达到这种情况。

操作数量刚好是 \(2n\)

#include <bits/stdc++.h>
using namespace std;
long long t,n,op[2200],p[2200],s[610][610];
int main()
{
	scanf("%lld",&t);
	while(t--)
	   {
	   	long long ans=0,cnt=0;
	   	scanf("%lld",&n);
	   	for(int i=n;i>=1;i--)
	   	    {
	   	    	op[++cnt]=1,p[cnt]=i;
	   	    	op[++cnt]=2,p[cnt]=i;
	   	    	for(int j=1;j<=n;j++)s[i][j]=j,s[j][i]=j;
			}
		for(int i=1;i<=n;i++)
		    for(int j=1;j<=n;j++)
			    ans+=s[i][j];
		printf("%lld %lld\n",ans,cnt);
		for(int i=1;i<=cnt;i++)
		    {
		    	printf("%lld %lld ",op[i],p[i]);
		    	for(long long j=1;j<=n;j++)printf("%lld ",j);
		    	printf("\n");
			}
       }
	return 0;
} 
posted @ 2025-02-16 22:31  w9095  阅读(7)  评论(0)    收藏  举报