# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
833407 | 2023-08-22T05:23:37 Z | rahulverma | Beautiful row (IZhO12_beauty) | Java 11 | 0 ms | 0 KB |
import java.util.*; public class beauty { static long[] a = new long[100]; static long[] b = new long[100]; static long[][] dp = new long[1 << 20][23]; static int n; static long rec(long mask, long last) { if (mask == ((1 << n) - 1)) return 1; if (last != -1 && dp[(int) mask][last] != -1) return dp[(int) mask][last]; long an = 0; for (int i = 0; i < n; i++) { if ((mask & (1 << i)) == 0) { if (last == -1 || a[(int) last] == a[i] || b[(int) last] == b[i]) { an += rec(mask | (1 << i), i); } } } return dp[(int) mask][last] = an; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i; long ans = 0; n = scanner.nextInt(); for (i = 0; i < n; i++) { long p = scanner.nextLong(); a[i] = Long.bitCount(p); long p1 = p; while (p1 > 0) { if (p1 % 3 == 1) b[i]++; p1 /= 3; } } for (i = 0; i < (1 << 20); i++) { Arrays.fill(dp[i], -1); } ans += rec(0, -1); System.out.println(ans); } }