# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
479847 | hjc4vr | 아름다운 순열 (IZhO12_beauty) | C++14 | 8 ms | 1612 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#define int long long
using namespace std;
int const maxn = 2005,MOD=1e9 + 7;
int d[maxn];
struct road{
int a,b,cost;
};
int pow(int a,int b){
int result = 1;
while (b>0){
if (b&1) {
result *= a;
}
a = a*a;
b = b>>1;
}
return result;
}
int gcd(int a,int b){
if (b==0) return a;
if (a<b) swap(a,b);
return gcd(b,a%b);
}
int count2(int x){
return __builtin_popcount(x);
}
int count3(int x){
int cnt=0;
while (x){
if (x%3==1) cnt++;
x/=3;
}
return cnt;
}
int dp[1<<20][21];
void solve(){
int n;cin>>n;
int arr[n+1];
for (int i=1;i<=n;++i) cin >> arr[i];
pair<int,int> v[n+1];
for (int i=1;i<=n;++i) v[i] = make_pair(count2(arr[i]),count3(arr[i]));
for (int mask=1;mask<(1<<n);++mask){
int sz=0;
int last=0;
for (int i=0;i<n;++i) if ((1<<i)&mask) sz++, last = i;
if (sz==1) dp[mask][last] = 1;
else{
for (int i=0;i<n;++i){
if ((1<<i)&mask){
int next_mask = (1<<i)^mask;
for (int j=0;j<n;++j){
if ((1<<j)&next_mask and (v[j+1].first==v[i+1].first or v[j+1].second==v[i+1].second)){
dp[mask][i] += dp[next_mask][j];
}
}
}
}
}
}
int ans =0;
for (int i=0;i<n;++i) ans += dp[(1<<n)-1][i];
cout << ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
solve();
}
//
//4
//4
//2
//2
//2
//4
//4
//4
//4
//2
//4
//4
//2
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |