답안 #873503

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873503 2023-11-15T07:57:23 Z Elvin_Fritl 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
754 ms 173076 KB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
///#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#include <bits/stdc++.h>
 
using namespace std;
 
/// toto -> 1
 
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define io                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define all(a) a.begin(),a.end()
#define rep(k,i,n) for(ll k=i;k<n;k++)
#define repp(k,i,n) for(ll k=n-1;k>=i;k--)
#define rech(k) for(char k='a';k<='z';k++)
#define SZ(a) (int)a.size()
#define MX(a) *max_element(all(a))
#define MN(a) *min_element(all(a))
#define SM(a) accumulate(all(a),0LL)
#define vi vector<int>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vvi vector<vector<int>>
 
/// toto -> 2
 
const int mod = 1e9 + 7 , modd = 998244353;
 
 
typedef long long ll;
 
void F1(bool res) {
    if(res) {
        cout<<"Yes\n" ;
    }
    else {
        cout<<"No\n" ;
    }
}
 
struct mint{
    ll val;
 
    mint(int64_t v = 0) {
        v %= mod;
        if (v < 0) v += mod;
        val = v;
    }
 
    mint& operator+=(const mint& other) {
        val += other.val;
        if (val >= mod) val -= mod;
        return *this;
    }
 
    mint& operator-=(const mint& other) {
        val += mod - other.val;
        if (val >= mod) val -= mod;
        return *this;
    }
 
    mint& operator*=(const mint& other) {
        val = (int64_t)val * other.val % mod;
        return *this;
    }
 
    mint& operator/=(const mint& other) {
        return *this *= other.inv();
    }
 
    friend mint operator+(mint a, const mint& b) { return a += b; }
    friend mint operator-(mint a, const mint& b) { return a -= b; }
    friend mint operator*(mint a, const mint& b) { return a *= b; }
    friend mint operator/(mint a, const mint& b) { return a /= b; }
 
    mint pow(int64_t exp) const {
        mint a = *this, res = 1;
        while (exp > 0) {
            if (exp & 1)
                res *= a;
            a *= a;
            exp >>= 1;
        }
        return res;
    }
 
    mint inv() const {
        assert(val != 0);
        return pow(mod - 2);
    }
 
    friend ostream& operator<<(ostream& os, const mint& m) {
        return os << m.val;
    }
};
 
ll bp(ll n,ll m){
    if(m == 0){
        return 1;
    }
    if(m == 1){
        return n%mod;
    }
    if(m%2==0){
        return bp(n*n%mod,m/2)%mod;
    }
    return n*bp(n,m-1)%mod;
}
 
 
/// toto -> 3
 
const int N = 21, M = 33, inf = 1e9 + 99;
const ll inff = 5e18;
 
ll func1(ll n){
    ll res = 0;
    while(n > 0){
        if(n%2 == 1){
			res++;
		}
        n /= 2;
    }
    return res;
}
 
ll func2(ll n){
    ll res = 0;
    while(n > 0){
        if(n % 3 == 1){
            res++;
		}
        n /= 3;
    }
    return res;
}
 
ll dp[(1<<N)][N];

void solve(){
	ll n; 
	cin >> n;
    ll v[n+1][n+1] , a[n+1];
    rep(i,1,n+1){
        cin >> a[i];
    }
    rep(i,1,n+1){
         rep(j,1,n+1){
            v[i][j] = 0;
            if(func1(a[i]) == func1(a[j])){
				v[i][j] = 1;
			}
            else if(func2(a[i]) == func2(a[j])){
				v[i][j] = 1;
			} 
        }
    }
     rep(i,0,n){
        dp[(1<<i)][i] = 1;
    }
    rep(i,1,(ll)(1<<n)){
        if(__builtin_popcount(i) == 1){
			continue;
		}
         rep(j,0,n){
            if((i&(1<<j))){
				 rep(u,0,n){
					if((i&(1<<u)) && j != u){
						if(v[j+1][u+1]){
							dp[i][j] += dp[i^(1<<j)][u];
						}
					}
				}
			}
        }
    }
    ll res = 0;
    rep(i,0,n){
        res += dp[(1<<n)-1][i];
    }
    cout << res << endl;;
	
}
 
int32_t main()
{
 
    io;
    
    ll t=1;
    ///  cin>>t;
    rep(_,1,t+1)
    {
        cerr << "Start of test case " << _ << "\n";
        ///cout<<"Case #"<<_<<": ";
        solve();
        cerr << endl;
        cerr << endl;
    }
    return 0;
 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 7 ms 4700 KB Output is correct
12 Correct 7 ms 4700 KB Output is correct
13 Correct 34 ms 12684 KB Output is correct
14 Correct 153 ms 43672 KB Output is correct
15 Correct 155 ms 43656 KB Output is correct
16 Correct 157 ms 43604 KB Output is correct
17 Correct 161 ms 43716 KB Output is correct
18 Correct 163 ms 43660 KB Output is correct
19 Correct 754 ms 173076 KB Output is correct
20 Correct 674 ms 173040 KB Output is correct