답안 #41217

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
41217 2018-02-14T03:26:38 Z MatheusLealV 아름다운 순열 (IZhO12_beauty) C++14
0 / 100
3000 ms 164828 KB
#include <bits/stdc++.h>
#define N 20
#define M ((1<<20))
using namespace std;
typedef long long ll;

int n, v[N], qtd[N][5], lista[N];

ll dp[N][M], pot[45];

inline bool equal(int a, int b)
{
	return (qtd[a][2] == qtd[b][2] || qtd[a][3] == qtd[b][3]);
}

vector<int> grafo[N];

int ternary(int x)
{
	int cnt = 0;

	for(int log3 = 30; log3 >= 0; log3 --)
	{
		ll aux = pot[log3], qaux = 0;

		if(x >= aux)
		{
			x -= aux;

			qaux ++;
		}

		if(x >= aux)
		{
			x -= aux;

			qaux ++;
		}

		cnt = cnt + (qaux == 1);
	}

	return cnt;
}

ll solve(int x, int mask)
{
	if(dp[x][mask] != -1) return dp[x][mask];

	if(mask == (1<<n) - 1) return 1;

	dp[x][mask] = 0;

	for(int v = 0; v < n; v++)
	{
		if(mask & (1<<v)) continue;

		if(qtd[x][2] == qtd[v][2] || qtd[x][3] == qtd[v][3]) dp[x][mask] += solve(v, mask | (1<<v));
	}

	return dp[x][mask];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n;

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

	pot[0] = 1;

	for(int i = 1; i < 45; i++) pot[i] = pot[i - 1]*3;

	for(int i = 0; i < n; i++)
	{
		qtd[i][2] = __builtin_popcount (v[i]);

		qtd[i][3] = ternary(v[i]);
	}

	memset(dp, -1, sizeof dp);

	ll best = 0;

	for(int i = 0; i < n; i++) best += solve(i, 1<<i);

	cout<<best<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 164472 KB Output is correct
2 Correct 114 ms 164556 KB Output is correct
3 Correct 122 ms 164556 KB Output is correct
4 Correct 118 ms 164668 KB Output is correct
5 Correct 124 ms 164772 KB Output is correct
6 Correct 119 ms 164772 KB Output is correct
7 Correct 121 ms 164772 KB Output is correct
8 Correct 121 ms 164772 KB Output is correct
9 Correct 117 ms 164828 KB Output is correct
10 Correct 118 ms 164828 KB Output is correct
11 Correct 132 ms 164828 KB Output is correct
12 Correct 126 ms 164828 KB Output is correct
13 Correct 196 ms 164828 KB Output is correct
14 Correct 546 ms 164828 KB Output is correct
15 Correct 659 ms 164828 KB Output is correct
16 Correct 463 ms 164828 KB Output is correct
17 Correct 621 ms 164828 KB Output is correct
18 Correct 408 ms 164828 KB Output is correct
19 Execution timed out 3023 ms 164828 KB Time limit exceeded
20 Halted 0 ms 0 KB -