답안 #898562

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898562 2024-01-04T22:10:54 Z n3rm1n 아름다운 순열 (IZhO12_beauty) C++17
0 / 100
0 ms 756 KB
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 25, MAXMASK = 1e6+10;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int n, a[MAXN], cnt2[MAXN], cnt3[MAXN];
int cnt_bin_2(int x)
{
    int cnt = 0;
    while(x)
    {
        if(x % 2 == 0)cnt ++;
        x /= 2;
    }
    return cnt;
}
int cnt_bin_3(int x)
{
    int cnt = 0;
    while(x)
    {
        if(x % 3 == 0)cnt ++;
        x /= 3;
    }
    return cnt;
}
int dp[MAXMASK][MAXN];
void read()
{
    cin >> n;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> a[i];
        cnt2[i] = cnt_bin_2(a[i]);
        cnt3[i] = cnt_bin_3(a[i]);

    }
}
void solve()
{
    for (int i = 1; i <= n; ++ i)
        dp[(1 << (i-1))][i] = 1;
    for (int mask = 1; mask < (1 << n); ++ mask)
    {
        for (int last = 1; last <= n; ++ last)
        {
            if(((1 << (last-1)) & mask) == 0)continue;
            for (int j = 1; j <= n; ++ j)
            {
                if(((1 << (j-1)) & mask) != 0)continue;
                if(cnt2[j] != cnt2[last] && cnt2[j] != cnt3[last] && cnt3[j] != cnt2[last] && cnt3[j] != cnt3[last])continue;
                int newmask = (mask | (1 << (j-1)));
                dp[newmask][j] += dp[mask][last];
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++ i)
    {
        ans += dp[(1 << n)-1][i];
    }
    cout << ans << endl;
}
int main()
{
    speed();
    read();
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 756 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -