답안 #636846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636846 2022-08-30T10:10:09 Z zeroesandones 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
399 ms 164456 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

#define FOR(i, j, k) for (ll i = j; i < (ll) k; ++i)
#define FORD(i, j, k) for (ll i = j; i >= (ll) k; --i)
#define nl "\n"
#define sp " "

#define all(x) (x).begin(), (x).end()
#define sc second
#define fr first
#define pb push_back

ll get(int x, int y) {
	ll res = 0;
	while(x) {
		res += (x % y == 1);
		x /= y;
	}
	return res;
}

void solve()
{
    int n;
    cin >> n;

    vi a(n);
    for(auto &i : a) {
		cin >> i;
	}
        
	bool spec[n][n] = {};

	FOR(i, 0, n) {
		FOR(j, 0, n) {
			spec[i][j] = (get(a[i], 2) == get(a[j], 2) || get(a[i], 3) == get(a[j], 3));
		}
	}

	ll dp[(1<<n)][n] = {};

	FOR(s, 0, (1<<n)) {
		FOR(i, 0, n) {
			ll curr = (1<<i);
			if((curr & s) == 0)
				continue;
			
			ll last = s ^ curr;
			if(last == 0) {
				dp[s][i] = 1;
				continue;
			}

			FOR(j, 0, n) {
				if(i == j) 
					continue;
				if(!spec[i][j])
					continue;
				dp[s][i] += dp[last][j];
				
			}
		}
	}

	ll ans = 0;
	FOR(i, 0, n) {
		ans += dp[(1 << n) - 1][i];
	}
	cout << ans << nl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 2004 KB Output is correct
12 Correct 4 ms 2004 KB Output is correct
13 Correct 17 ms 8404 KB Output is correct
14 Correct 83 ms 37244 KB Output is correct
15 Correct 97 ms 37236 KB Output is correct
16 Correct 95 ms 37232 KB Output is correct
17 Correct 98 ms 37232 KB Output is correct
18 Correct 88 ms 37232 KB Output is correct
19 Correct 394 ms 164400 KB Output is correct
20 Correct 399 ms 164456 KB Output is correct