답안 #106207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106207 2019-04-17T10:44:58 Z Hideo 아름다운 순열 (IZhO12_beauty) C++14
0 / 100
25 ms 6784 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define vl vector < ll >
#define pi pair < int, int >
#define pii pair < int, pi >
#define vii vector < pi >

const int N = 1e5 + 7;
const int mod = 1e9 + 7;

int b[50], t[50];
int n;

ll dp[(1 << 22)][50];
ll ans;

int bin (int x, int c = 0){
    if (x % 2 == 1)
        c++;
    if (!x)
        return c;
    bin(x / 2, c);
}

int ter (int x, int c = 0){
    if (x % 3 == 1)
        c++;
    if (!x)
        return c;
    ter(x / 3, c);
}

main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        int a;
        cin >> a;
        b[i] = bin(a);
        t[i] = ter(a);
    }
    for (int i = 0; i < n; i++)
        dp[(1 << i)][i] = 1;

    for (int val = 2; val <= n; val++){
        for (int mask = 1; mask < (1 << n); mask++){
            int cnt = 0;
            for (int sub = 0; sub < n; sub++)
                if (mask & (1 << sub))
                    cnt++;
            if (cnt != val)
                continue;
            for (int sub = 0; sub < n; sub++){
                if (mask & (1 << sub)){
                    int submask = (mask ^ (1 << sub));
                    for (int last = 0; last < n; last++)
                        if (last != sub && submask & (1 << last) && (b[last] == b[sub] || t[last] == t[sub]))
                            dp[mask][sub] += dp[submask][last];
                    dp[mask][sub] %= mod;
                }
            }
            if (val == n){
                for (int last = 0; last < n; last++)
                    ans += dp[mask][last];
                cout << ans % mod;
            }
        }
    }
}

Compilation message

beauty.cpp:40:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
beauty.cpp: In function 'int bin(int, int)':
beauty.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
beauty.cpp: In function 'int ter(int, int)':
beauty.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 3 ms 768 KB Output is correct
8 Correct 3 ms 768 KB Output is correct
9 Correct 3 ms 768 KB Output is correct
10 Correct 4 ms 768 KB Output is correct
11 Incorrect 25 ms 6784 KB Output isn't correct
12 Halted 0 ms 0 KB -