UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#202060#3519. A Permutation Problemshr10013527ms130748kbC++11773b2024-02-11 10:17:452024-02-11 13:09:29

answer

#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
const int N=55,mod=1e9+7;
int n,f=1,ans,a[N],p[N];
ull al;
unordered_map<ull,int> mp;
int dfs(int x,ull s,ull b){
	if(x>n) return 1;
	ull t=~s&al;
	if(__builtin_popcountll(t)<=n-x) return 0;
	if(mp.count(b)) return mp[b];
	ull sum=0;
	auto calc=[&](int v){
		if(s>>v&1) return;
		ull nb=b|1llu<<(n-v),ns=s;
		if(v*2>n) ns|=nb<<(v*2-n);
		else ns|=nb>>(n-v*2);
		sum+=dfs(x+1,ns,nb);
	};
	if(!p[x]) for(;t;t-=t&-t) calc(__lg(t&-t));
	else if(t>>p[x]&1) calc(p[x]);
	return mp[b]=sum%mod;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>p[i];
	for(int i=1,cnt=0;i<=n;i++) if(!p[i]) f=1ll*f*++cnt%mod;
	al=(1llu<<(n+1))-2;
	cout<<(f-dfs(1,0,0)+mod)%mod;
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 1244kb

input:

5
0 0 0 3 0

output:

24

result:

ok 1 number(s): "24"

Test #2:

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

input:

5
5 0 0 0 0

output:

22

result:

ok 1 number(s): "22"

Test #3:

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

input:

5
0 0 0 0 0

output:

100

result:

ok 1 number(s): "100"

Subtask #2:

score: 10
Accepted

Test #4:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

9
0 0 0 0 0 0 0 0 0

output:

362384

result:

ok 1 number(s): "362384"

Test #5:

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

input:

10
0 0 0 0 0 1 0 3 0 0

output:

40318

result:

ok 1 number(s): "40318"

Test #6:

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

input:

10
0 0 0 0 0 0 0 0 0 0

output:

3627734

result:

ok 1 number(s): "3627734"

Subtask #3:

score: 10
Accepted

Test #7:

score: 10
Accepted
time: 0ms
memory: 1352kb

input:

15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

674283947

result:

ok 1 number(s): "674283947"

Test #8:

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

input:

14
0 0 0 7 0 0 0 0 0 0 0 0 0 0

output:

227020758

result:

ok 1 number(s): "227020758"

Test #9:

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

input:

15
15 0 0 0 1 0 0 0 0 0 3 0 0 0 0

output:

479001600

result:

ok 1 number(s): "479001600"

Subtask #4:

score: 10
Accepted

Test #10:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

20
20 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 19

output:

425606191

result:

ok 1 number(s): "425606191"

Test #11:

score: 0
Accepted
time: 4ms
memory: 1588kb

input:

20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

143388927

result:

ok 1 number(s): "143388927"

Test #12:

score: 0
Accepted
time: 1ms
memory: 1308kb

input:

20
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

557126181

result:

ok 1 number(s): "557126181"

Subtask #5:

score: 10
Accepted

Test #13:

score: 10
Accepted
time: 5ms
memory: 1592kb

input:

25
0 0 0 0 0 0 18 0 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 16

output:

602640637

result:

ok 1 number(s): "602640637"

Test #14:

score: 0
Accepted
time: 12ms
memory: 2892kb

input:

25
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0

output:

643345452

result:

ok 1 number(s): "643345452"

Test #15:

score: 0
Accepted
time: 13ms
memory: 2892kb

input:

25
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

242322220

result:

ok 1 number(s): "242322220"

Subtask #6:

score: 10
Accepted

Test #16:

score: 10
Accepted
time: 42ms
memory: 5164kb

input:

30
0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 30 0 0 0 0 0 0

output:

35757887

result:

ok 1 number(s): "35757887"

Test #17:

score: 0
Accepted
time: 5ms
memory: 2120kb

input:

30
0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

597740103

result:

ok 1 number(s): "597740103"

Test #18:

score: 0
Accepted
time: 41ms
memory: 5432kb

input:

30
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

343955582

result:

ok 1 number(s): "343955582"

Subtask #7:

score: 10
Accepted

Test #19:

score: 10
Accepted
time: 150ms
memory: 15244kb

input:

35
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

478043548

result:

ok 1 number(s): "478043548"

Test #20:

score: 0
Accepted
time: 14ms
memory: 2888kb

input:

34
0 0 0 0 0 25 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

408748903

result:

ok 1 number(s): "408748903"

Test #21:

score: 0
Accepted
time: 152ms
memory: 15048kb

input:

35
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0

output:

943272305

result:

ok 1 number(s): "943272305"

Subtask #8:

score: 10
Accepted

Test #22:

score: 10
Accepted
time: 407ms
memory: 30440kb

input:

40
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

614960773

result:

ok 1 number(s): "614960773"

Test #23:

score: 0
Accepted
time: 409ms
memory: 30440kb

input:

40
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22

output:

72581330

result:

ok 1 number(s): "72581330"

Test #24:

score: 0
Accepted
time: 402ms
memory: 29912kb

input:

40
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 39 0 22 0 0 18 0 0 0 0 0 0 0 0

output:

354551275

result:

ok 1 number(s): "354551275"

Subtask #9:

score: 10
Accepted

Test #25:

score: 10
Accepted
time: 943ms
memory: 59152kb

input:

45
0 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0

output:

626855450

result:

ok 1 number(s): "626855450"

Test #26:

score: 0
Accepted
time: 1102ms
memory: 65200kb

input:

45
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 0 0 0 0 0 0 0 0

output:

60774616

result:

ok 1 number(s): "60774616"

Test #27:

score: 0
Accepted
time: 272ms
memory: 19592kb

input:

45
0 0 0 42 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

952787165

result:

ok 1 number(s): "952787165"

Subtask #10:

score: 5
Accepted

Test #28:

score: 5
Accepted
time: 1440ms
memory: 80512kb

input:

47
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

986042227

result:

ok 1 number(s): "986042227"

Subtask #11:

score: 5
Accepted

Test #29:

score: 5
Accepted
time: 897ms
memory: 59656kb

input:

50
0 0 0 0 0 0 0 0 43 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

output:

82508855

result:

ok 1 number(s): "82508855"

Test #30:

score: 0
Accepted
time: 2530ms
memory: 130748kb

input:

50
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

333255637

result:

ok 1 number(s): "333255637"

Test #31:

score: 0
Accepted
time: 2234ms
memory: 120456kb

input:

49
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

output:

22735711

result:

ok 1 number(s): "22735711"

Test #32:

score: 0
Accepted
time: 2452ms
memory: 128108kb

input:

50
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

output:

839694966

result:

ok 1 number(s): "839694966"

Extra Test:

score: 0
Extra Test Passed