Submission #889849

#TimeUsernameProblemLanguageResultExecution timeMemory
889849heeheeheehaawBeautiful row (IZhO12_beauty)C++17
100 / 100
937 ms172672 KiB
#include <iostream> #define int long long using namespace std; long long dp[1049000][21]; int v[25], nrr2[25], nrr3[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], nrr2[i] = nr2(v[i]), nrr3[i] = nr3(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(nrr3[j] == nrr3[i] || nrr2[j] == nrr2[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; }
#Verdict Execution timeMemoryGrader output
Fetching results...