跳到正文

题解

P9752 [CSP-S 2023] 密码锁

无钩七不改名约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年10月22日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 3 分钟
题目
P9752

版本

  1. v1当前

简单暴力。

赛时看错题浪费了半个多小时。

我们可以直接开一个六维数组 bi,k1,k2,k3,k4,k5b_{i,k1,k2,k3,k4,k5} 表示第 ii 种假密码能否转为真密码 k1  k2  k3  k4  k5\tt{k1\;k2\;k3\;k4\;k5}。这个根据题意简单模拟就好了。

然后枚举每种可能出现的真密码,如果一个真密码是每种假密码都可以转出的,计入答案。

然后就做完了。

代码如下:

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

const int N=10;

int n,a[N][N];
bool b[N][N][N][N][N][N];
int qwq[N];
long long ans;

int read(){
	int f=1,k=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		k=k*10+ch-'0';
		ch=getchar();
	}
	return f*k;
}

void dfs(int x){
	if(x>5){
		for(int i(1);i<=n;++i)
			if(!b[i][qwq[1]][qwq[2]][qwq[3]][qwq[4]][qwq[5]])return;
		++ans;
		return;
	}
	for(int i(0);i<=9;++i){
		qwq[x]=i;
		dfs(x+1);
	}
	return;
}

int main(){
//	freopen("lock.in","r",stdin);
//	freopen("lock.out","w",stdout);
	n=read();
	for(int i(1);i<=n;++i){
		for(int j(1);j<=5;++j)a[i][j]=read();
		for(int j(1);j<=5;++j){
			int tmp=a[i][j];
			for(int k(0);k<=9;++k){
				if(k==tmp)continue;
				a[i][j]=k;
				b[i][a[i][1]][a[i][2]][a[i][3]][a[i][4]][a[i][5]]=1;
			}
			a[i][j]=tmp;
		}
		for(int j(1);j<5;++j){
			int tmp1=a[i][j],tmp2=a[i][j+1];
			for(int k(0);k<=9;++k){
				if(k==tmp1)continue;
				a[i][j]=k;
				a[i][j+1]=k+(tmp2-tmp1);
				if(a[i][j+1]<0)a[i][j+1]+=10;
				else if(a[i][j+1]>9)a[i][j+1]-=10;
				b[i][a[i][1]][a[i][2]][a[i][3]][a[i][4]][a[i][5]]=1;
			}
			a[i][j]=tmp1;a[i][j+1]=tmp2;
		}
	}
	dfs(1);
	printf("%lld",ans);
	return 0;
}
原文