QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424736 | #5954. Power Swapper | hai | 0 | 0ms | 0kb | C++14 | 1.8kb | 2024-05-29 16:27:58 | 2024-05-29 16:27:58 |
answer
# include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v;
long long jie[15];
long long dfs(vector<int> v,int n,int k)
{
if(n==0)
return jie[k];
int pos=0;
for(int i=0;i<1<<n;i+=2)
if(!(v[i]%2 && v[i+1]==v[i]+1))
pos++;
if(pos==0 || pos==1)
{
vector<int> v2(1<<(n-1));
for(int i=0;i<1<<n;i+=2)
v2[i/2]=(v[i]+1)/2;
return dfs(v2,n-1,k+pos);
}
else if(pos==2)
{
vector<int> v2(1<<(n-1));
int pos1=-1,pos2=-1;
for(int i=0;i<1<<n;i+=2)
if(!(v[i]%2 && v[i+1]==v[i]+1))
{
if(pos1==-1)
pos1=i;
else
pos2=i;
}
long long ans=0;
for(int p1=pos1;p1<=pos1+1;p1++)
for(int p2=pos2;p2<=pos2+1;p2++)
{
swap(v[p1],v[p2]);
bool flag=0;
for(int i=0;i<1<<n;i+=2)
if(!(v[i]%2 && v[i+1]==v[i]+1))
{
flag=1;
break;
}
if(!flag)
{
for(int i=0;i<1<<n;i+=2)
v2[i]=v[i+1]/2;
ans+=dfs(v2,n-1,k+1);
}
swap(v[p1],v[p2]);
}
return ans;
}
else
return 0;
}
void solve(int testid)
{
cin>>n;
v.resize(1<<n);
for(int i=0;i<1<<n;i++)
cin>>v[i];
cout<<"Case #"<<testid<<": "<<dfs(v,n,0)<<endl;
}
int main()
{
jie[0]=1;
for(int i=1;i<=12;i++)
jie[i]=jie[i-1]*i;
int T;
cin>>T;
for(int t=1;t<=T;t++)
solve(t);
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
200 1 2 1 2 1 4 3 2 3 7 8 5 6 1 2 4 3 2 4 3 2 1 2 2 4 1 3 4 3 10 11 12 1 2 9 4 13 14 15 16 5 6 7 8 1 2 1 1 2 1 1 2 1 1 1 2 4 1 2 3 4 13 14 15 16 9 12 11 10 5 6 7 8 4 9 10 11 12 1 2 15 16 13 14 6 4 5 3 7 8 2 1 4 3 2 2 3 2 1 4 4 1 2 3 4 13 14 15 16 9 10 11 8 5 6 7 12 4 13 14 11 12 1 2 5 4 9 10 15 16 3...
output:
Case #1: 1 Case #2: 4 Case #3: 6 Case #4: 0 Case #5: 2
result:
Subtask #2:
score: 0
Runtime Error
Test #2:
score: 0
Runtime Error
input:
200 1 2 1 2 1 4 3 2 3 7 8 5 6 1 2 4 3 2 4 3 2 1 6 33 34 35 36 37 38 39 40 41 42 43 44 19 20 51 52 47 10 11 12 13 14 15 16 57 58 59 60 61 62 63 64 1 2 3 4 5 6 7 8 45 46 9 48 53 54 55 56 17 18 49 50 21 22 23 24 25 26 27 28 29 30 31 32 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2...
output:
Case #1: 1 Case #2: 4 Case #3: 6 Case #4: 0