# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
339654 | Ahmad_Hasan | Beautiful row (IZhO12_beauty) | C++17 | 1 ms | 492 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> nums;
int cnt=0ll;
vector<vector<int> > dp;
int slv(int msk=0,int lstidx=-1){
if(msk==(1ll<<n)-1ll){
return 1ll;
}
int cnt=0;
if(lstidx!=-1&&dp[msk][lstidx]!=-1)
return dp[msk][lstidx];
for(int i=0;i<n;i++){
if(!(msk&(1<<i))){
if(lstidx==-1){
cnt+=slv(msk|(1ll<<i),i);
continue;
}
int f=0;
{
int num1=nums[lstidx];
int num2=nums[i];
int cntb2=0;
while(num1||num2){
cntb2-=num1%2ll;
cntb2+=num2%2ll;
num1/=2ll;
num2/=2ll;
}
if(!cntb2){
f=1;
}
num1=nums[lstidx];
num2=nums[i];
int cntb3=0;
while(num1||num2){
cntb3-=(long long)(num1%3ll==1ll);
cntb3+=(long long)(num2%3ll==1ll);
num1/=3ll;
num2/=3ll;
}
if(!cntb3){
f=1;
}
}
if(f){
cnt+=slv(msk|(1ll<<i),i);
}
}
}
return dp[msk][lstidx]=cnt;
}
int32_t main()
{
/**ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);*/
cin>>n;
nums=vector<int>(n);
for(int i=0;i<n;i++){
cin>>nums[i];
}
dp=vector<vector<int> >((1<<n),vector<int>(n,-1ll));
for(int i=(1<<n)-1;i>=1;i--){
for(int j=n-1;j>=0;j--){
if(i&(1<<j))
slv(i,j);
}
}
for(int i=0;i<n;i++)
slv((1<<i),i);
cout<<(long long)slv();
return 0;
}
/****
1
8 4
3 1
4 3
1 8
8 7
1
100 9
1 100
2 1
3 2
4 3
5 4
100 99
99 98
98 97
97 96
19 3 6
3 12 9
2000000000 1000000000 1000000000
3 1000000000 1000000000
15
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |