跳到正文

题解

题解:B3761 [信息与未来 2021] 三角魔方

__xiaoY约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年7月26日
更新
2026年7月26日
篇幅
约 6 分钟
题目
B3761

版本

  1. v1当前

题意

B3761

对一个有 16 个字母(A~P)的三角魔方,按给定操作序列执行 aba^b 次操作。输出最终字母排列,一行 16 个字母。

解法

1. 定义

可以直接定义 16 个字母和 1 个辅助变量。

C++
char swp,A='A',B='B',C='C',D='D',E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L',M='M',N='N',O='O',P='P';
//swp是辅助旋转操作的

2.转动函数

按题意赋值即可,以 U 操作为例:

C++
void _U(int x)
{
	if(x==1) return;
	if(x==3) swp=E,E=K,K=L,L=swp;
	if(x==5) swp=B,B=F,F=G,G=M,M=N,N=swp;
	if(x==7) swp=A,A=C,C=D,D=H,H=I,I=O,O=P,P=swp;
    return;
}

R,D 操作与 U 类似:

R,D
C++
void _R(int x)
{
	if(x==1) return;
	if(x==3) swp=D,D=C,C=B,B=swp;
	if(x==5) swp=I,I=H,H=G,G=F,F=E,E=swp;
	if(x==7) swp=P,P=O,O=N,N=M,M=L,L=K,K=J,J=swp;
    return;
}
void _D(int x)
{
	if(x==1) return;
	if(x==3) swp=N,N=O,O=I,I=swp;
	if(x==5) swp=L,L=M,M=G,G=H,H=D,D=swp;
	if(x==7) swp=J,J=K,K=E,E=F,F=B,B=C,C=A,A=swp;
	return;
}

然后三种操作用函数在封装一下。

C++
void change(string s)
{
	if(s[0]=='U') _U(s[1]-'0');
	if(s[0]=='R') _R(s[1]-'0');
	if(s[0]=='D') _D(s[1]-'0');
}

3.记录周期

上述代码已经可以完成旋转操作了,接下来处理 aa,bb 的问题。

显然,aba^b 暴力是不可行的。

注意到以相同序列转动魔方会产生循环,于是我们可以找出循环后,对循环外余下的操作暴力模拟。

找循环很简单,就是先模拟一次循环,重复进行转动操作直到恢复原状(A~P)为止。

C++
bool check()
{
	if(A!='A' or B!='B' or C!='C' or D!='D' or E!='E' or F!='F' or G!='G' or H!='H' or I!='I' or J!='J' or K!='K' or L!='L' or M!='M' or N!='N' or O!='O' or P!='P') return 0;
	return 1;
}//有点不美观,但是很直观
do{
  for(int i=0;i<m.size();i++) change(m[i]);//m是vector,记录了每次操作的类型与编号
  cnt++;//记录次数
}while(!check());

思路就这么简单,下面是AC代码。

AC code
C++
#include<bits/stdc++.h>
using namespace std;

vector<string> m;
int a,b;
int sum=1;
char swp,A='A',B='B',C='C',D='D',E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L',M='M',N='N',O='O',P='P';

void _U(int x)
{
	if(x==1) return;
	if(x==3) swp=E,E=K,K=L,L=swp;
	if(x==5) swp=B,B=F,F=G,G=M,M=N,N=swp;
	if(x==7) swp=A,A=C,C=D,D=H,H=I,I=O,O=P,P=swp;
    return;
}
void _R(int x)
{
	if(x==1) return;
	if(x==3) swp=D,D=C,C=B,B=swp;
	if(x==5) swp=I,I=H,H=G,G=F,F=E,E=swp;
	if(x==7) swp=P,P=O,O=N,N=M,M=L,L=K,K=J,J=swp;
    return;
}
void _D(int x)
{
	if(x==1) return;
	if(x==3) swp=N,N=O,O=I,I=swp;
	if(x==5) swp=L,L=M,M=G,G=H,H=D,D=swp;
	if(x==7) swp=J,J=K,K=E,E=F,F=B,B=C,C=A,A=swp;
	return;
}
bool check()
{
	if(A!='A' or B!='B' or C!='C' or D!='D' or E!='E' or F!='F' or G!='G' or H!='H' or I!='I' or J!='J' or K!='K' or L!='L' or M!='M' or N!='N' or O!='O' or P!='P') return 0;
	return 1;
}
void change(string s)
{
	if(s[0]=='U') _U(s[1]-'0');
	if(s[0]=='R') _R(s[1]-'0');
	if(s[0]=='D') _D(s[1]-'0');
}
int main()
{
	string s;
	cin>>s;
	cin>>a>>b;
	int pos=0,cnt=0;
	while(pos<s.size())//这里是把操作序列拆开装进 m 里
	{
		string tmp="";
		tmp+=s[pos],tmp+=s[pos+1];
		m.push_back(tmp);
		pos+=2;
	}
	
	do{
		for(int i=0;i<m.size();i++) change(m[i]);
		cnt++;
	}while(!check());
	for(int i=1;i<=B;i++) sum=(sum*A)%cnt;
	while(sum--)
		for(int i=0;i<m.size();i++) change(m[i]);
		
	cout<<A<<B<<C<<D<<E<<F<<G<<H<<I<<J<<K<<L<<M<<N<<O<<P;
	return 0;
}

恭喜你又水了一道绿题^_^

原文