Submission #758338

# Submission time Handle Problem Language Result Execution time Memory
758338 2023-06-14T13:11:37 Z Ahmed_Solyman Beautiful row (IZhO12_beauty) C++14
0 / 100
80 ms 164452 KB
#include <bits/stdc++.h>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
    return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
    cout<<(x?"YES":"NO")<<endl;
}
int ones2(ll n){
    return __builtin_popcount(n);
}
int ones3(ll n){
    int cnt=0;
    while(n){
        cnt+=(n%3==1);
        n/=3;
    }
    return cnt;
}
const int N=20;
ll dp[N][(1<<N)],arr[N],n;
int solve(int i,int mask){
    if(ones2(mask)==n){
        return 1;
    }
    ll &ret=dp[i][mask];
    if(~ret)return ret;
    ret=0;
    for(int j=0;j<n;j++){
        if((1<<j)&mask)continue;
        if(mask){
            if(ones2(arr[j])==ones2(arr[i]) || ones3(arr[j])==ones3(arr[i])){
                ret+=solve(j,mask|(1<<j));
            }
        }
        else{
            ret+=solve(j,mask|(1<<j));
        }
    }
    return ret;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    fast
    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    fil(dp,-1);
    cout<<solve(0,0)<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 61 ms 164452 KB Output is correct
2 Correct 67 ms 164452 KB Output is correct
3 Correct 62 ms 164424 KB Output is correct
4 Correct 62 ms 164344 KB Output is correct
5 Correct 63 ms 164432 KB Output is correct
6 Correct 65 ms 164448 KB Output is correct
7 Correct 61 ms 164416 KB Output is correct
8 Correct 63 ms 164428 KB Output is correct
9 Correct 61 ms 164432 KB Output is correct
10 Correct 62 ms 164448 KB Output is correct
11 Incorrect 80 ms 164380 KB Output isn't correct
12 Halted 0 ms 0 KB -