| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 163498 | tselmegkh | 아름다운 순열 (IZhO12_beauty) | C++14 | 3061 ms | 239496 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
vector<int> binary[33], trinary[33];
vector<int> a;
int n;
bool same[25], block[25];
int count(int x){
int res = 0;
while(x > 0){
if(x % 3 == 1){
res++;
}
x /= 3;
}
return res;
}
unordered_map<string, bool> calc;
int solve(int mask, string cur, int last){
if(mask == (1 << n) - 1){
//cout << cur << '\n';
if(calc[cur])return 0;
calc[cur] = 1;
return 1;
}
if(calc[cur])return 0;
calc[cur] = 1;
int ans = 0;
if(mask == 0){
for(int j = 0; j < n; j++){
int curmask = mask | (1 << j);
string tmp;
tmp += a[j] + '0';
ans += solve(curmask, tmp, j);
}
}else{
int binaryones = __builtin_popcount(a[last]), trinaryones = count(a[last]);
for(int j = 0; j < binary[binaryones].size(); j++){
int k = binary[binaryones][j];
if(mask & (1 << k))continue;
int curmask = mask | (1 << k);
string tmp = cur;
tmp += a[k] + '0';
ans += solve(curmask, tmp, k);
}
for(int j = 0; j < trinary[trinaryones].size(); j++){
int k = trinary[trinaryones][j];
if(mask & (1 << k))continue;
string tmp = cur;
tmp += a[k] + '0';
int curmask = mask | (1 << k);
ans += solve(curmask, tmp, k);
}
}
return ans;
}
int main(){
cin >> n;
a.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i];
int cnt3 = count(a[i]), cnt2 = __builtin_popcount(a[i]);
binary[cnt2].push_back(i);
trinary[cnt3].push_back(i);
}
cout << solve(0, "", -1) << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
