Submission #701985

# Submission time Handle Problem Language Result Execution time Memory
701985 2023-02-22T12:30:31 Z Darren0724 Beautiful row (IZhO12_beauty) C++17
100 / 100
1500 ms 172748 KB
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define int long long


signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    int n;cin>>n;
    vector<int> v(n);
    vector<int> a(n),b(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    for(int i=0;i<n;i++){
        int p=v[i];
        while(p>0){
            if(p%3==1){
                a[i]++;
            }
            p/=3;
        }
        p=v[i];
        while(p>0){
            if(p%2==1){
                b[i]++;
            }
            p/=2;
        }
    }
    vector<vector<int>> dp(n,vector<int>(1<<n));
    for(int i=0;i<n;i++){
        dp[i][1<<i]=1;
    }
    for(int i=1;i<(1<<n);i++){
        for(int j=0;j<n;j++){
            if(i&(1<<j)){
                for(int k1=0;k1<n;k1++){
                    if(a[j]==a[k1]||b[j]==b[k1]){
                        dp[j][i]+=dp[k1][i-(1<<j)];
                    }
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        ans+=dp[i][(1<<n)-1];
    }
    cout<<ans<<endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 7 ms 2260 KB Output is correct
12 Correct 6 ms 2260 KB Output is correct
13 Correct 51 ms 9044 KB Output is correct
14 Correct 266 ms 39380 KB Output is correct
15 Correct 312 ms 39380 KB Output is correct
16 Correct 208 ms 39300 KB Output is correct
17 Correct 261 ms 39380 KB Output is correct
18 Correct 178 ms 39380 KB Output is correct
19 Correct 1204 ms 172748 KB Output is correct
20 Correct 1500 ms 172748 KB Output is correct