답안 #889841

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
889841 2023-12-20T08:06:59 Z heeheeheehaaw 아름다운 순열 (IZhO12_beauty) C++17
0 / 100
3000 ms 86984 KB
#include <iostream>
#define int long long

using namespace std;

int dp[1049000][21];
int v[25];

int nr2(int n)
{
    int nr = 0;
    while(n >= 1)
    {
        if(n % 2 == 1)
            nr++;
        n /= 2;
    }

    return nr;
}

int nr3(int n)
{
    int nr = 0;
    while(n >= 1)
    {
        if(n % 3 == 1)
            nr++;
        n /= 3;
    }

    return nr;
}

signed main()
{
    int n;
    cin>>n;
    for(int i = 0; i < n; i++)
        cin>>v[i];

    for(int mask = 1; mask < (1 << n); mask++)
    {
        int nr = 0, b = 0;
        for(int i = 0; i < n; i++)
            if(((1 << i) & mask) != 0)
                nr++, b = i;
        if(nr == 1)
        {
            dp[mask][b] = 1;
        }
        else
        {
            for(int i = 0; i < n; i++)
            {
                if(((1 << i) & mask) != 0)
                {
                    for(int j = 0; j < n; j++)
                    {
                        if(((1 << j) & (mask ^ (1 << i))) == 0)
                            continue;
                        if(nr3(v[j]) == nr3(v[i]) || nr2(v[j]) == nr2(v[i]))
                        {
                            dp[mask][i] += dp[(mask ^ (1 << i))][j];
                        }
                    }
                }
            }
        }
    }

    int rez = 0;
    for(int i = 0; i < n; i++)
        rez += dp[(1 << n) - 1][i];
    cout<<rez;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 448 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 65 ms 4692 KB Output is correct
12 Correct 68 ms 4688 KB Output is correct
13 Correct 328 ms 13140 KB Output is correct
14 Correct 1742 ms 43900 KB Output is correct
15 Correct 1689 ms 43900 KB Output is correct
16 Correct 1628 ms 43900 KB Output is correct
17 Correct 1413 ms 43888 KB Output is correct
18 Correct 1577 ms 43872 KB Output is correct
19 Execution timed out 3039 ms 86984 KB Time limit exceeded
20 Halted 0 ms 0 KB -