QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#466900#7687. Randias Permutation Taskyurongyi#TL 1ms5576kbC++142.6kb2024-07-08 11:47:272024-07-08 11:47:27

Judging History

你现在查看的是最新测评结果

  • [2024-07-08 11:47:27]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5576kb
  • [2024-07-08 11:47:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[181][181],sz,flag,b[181],je[189],ans,dp[500005][2];
const long long mod=1e9+7;
map<long long,bool> mm,nn;
void dfs(int x){
//	cout<<x<<endl;
	if(x>=m+1){
		long long sum=0,sm=0;
		for(int i=1;i<=n;++i){
			sum=(sum+(b[i]*(je[n-i+1]))%mod)%mod;
			sm=(sm+b[i]*(i+je[n-i]))%mod;
//			cout<<b[i]<<' ';
		}
//		cout<<sum<<' '<<sm<<endl;
		int fi=0;
		if(mm[sum]!=1)mm[sum]=1,fi++;
		if(nn[sm]!=1)nn[sm]=1,fi++;
		if(fi>=1)ans++;
		return;
	}
	int c[181];
	for(int i=1;i<=n;++i){
		c[i]=b[i];
	}
	dfs(x+1);
//	for(int i=1;i<=n;++i)cout<<c[i]<<endl;
	for(int i=1;i<=n;++i){
		b[i]=c[a[x][i]];//cout<<a[x][i]<<endl;
	}
	bool fg=1;
	for(int i=1;i<=n;++i)if(b[i]!=i)fg=0;
	if(fg)flag=1;
	dfs(x+1);
	return;
}
int main(){
	cin>>n>>m;
	je[0]=1;
	for(int i=1;i<=188;++i)je[i]=je[i-1]*i%mod;
	for(int i=1;i<=m;++i){
		for(int j=1;j<=n;++j){
			cin>>a[i][j];
		}
	}
	if(n<=0){
		for(int i=1;i<=n;++i)a[0][i]=i;
		for(int i=1;i<=1;++i){
			int sum=0,e[10],d[10]={0,1,2,3,4,5,6,7,8,9};
			for(int k=1;k<=n;++k){
				e[k]=a[0][k];
				int o=0,nw=0;
				while(d[nw]!=e[k]){
					if(d[nw]!=0)o++;
					nw++;
				}
				d[nw]=0;
				sum+=o*je[n-k];
			}
			dp[sum][1]=1;
			sz=sum;
		}
//		cout<<"SZ="<<sz<<endl;
		for(int i=1;i<=m;++i){
			for(int j=0;j<=je[n];++j)dp[j][(i+1)%2]=0;
//			for(int j=0;j<=je[n];++j)cout<<dp[j][i%2]<<' ';
//			cout<<endl;
			for(int j=0;j<=je[n];++j){
//				cout<<j<<' '<<je[n]<<endl;
				if(dp[j][i%2]==0)continue;
				int c[10],e[10],sum=0,d[10]={0,1,2,3,4,5,6,7,8,9};
				for(int k=1;k<=n;++k){
					int l=(j/je[n-k])%(n+1-k);
					int o=1;
//					cout<<l<<' '<<o<<' '<<k<<endl;
					while(l>=0){
						if(d[o]>0&&l>0)l--;
						else if(d[o]>0)c[k]=d[o],d[o]=0,l--;
						o++;
					}
//					cout<<"DONE"<<endl;
				}
//				cout<<j<<' ';
//				for(int p=1;p<=n;++p)cout<<c[p]<<' ';
//				cout<<endl;
				for(int p=0;p<=9;++p)d[p]=p;
				dp[j][(i+1)%2]=1;
				for(int k=1;k<=n;++k){
					int o=0,nw=0;
					e[k]=c[a[i][k]];
					while(d[nw]!=e[k]){
						if(d[nw]!=0)o++;
						nw++;
					}
					d[nw]=0; 
//					cout<<e[k]<<' '<<o<<' ';
					sum+=o*je[n-k];
				}
//				cout<<sum<<endl;
				if(sum==sz)flag=1;
				dp[sum][(i+1)%2]=1;
			}
		}
		for(int i=1;i<=je[n];++i){
			if(dp[i][(m+1)%2]!=0)ans++;
		}
		cout<<(ans+flag)%mod<<endl;
	}else{
		for(int j=1;j<=n;++j)b[j]=j;
		dfs(1);
		cout<<(ans-1+flag)%mod<<endl;
	}
	return 0;
}
/*
8 3
1 2 3 4 5 6 8 7 
1 2 3 4 5 7 8 6
1 2 3 4 5 8 7 6 
*/
/*
5 5
1 2 3 4 5
1 2 3 4 5 
1 2 4 3 5
1 2 3 5 4
1 2 5 3 4
*/ 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5576kb

input:

5 4
1 2 3 4 5
5 1 3 4 2
3 4 1 5 2
5 2 4 1 3

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

2 1
2 1

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: -100
Time Limit Exceeded

input:

1 180
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

output:


result: