1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <bits/stdc++.h> using namespace std;
const int N = 16;
inline int read() { int s(0), w(1); char ch = getchar(); while (ch < '0' || ch > '9'){if (ch == '-') w = -1; ch = getchar();} while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; }
inline void put(int x) { if (! x) putchar('0'); if (x < 0) putchar('-'), x = -x; int num(0); char c[66]; while (x) c[++ num] = x % 10 + 48, x /= 10; while (num) putchar(c[num --]); return (void)(putchar('\n')); }
int n, res, sx, sy, s; int a[N];
inline int cmp(int x, int y) { return x > y; }
inline void dfs(int x, int y, int fuck) {
if ((n - y + 1) * 3 < a[x]) return; if (x == n) if (! a[x]) return (void)(++ res); if (y == n + 1) if (a[x] == 0) return (void)(dfs(x + 1, x + 2, fuck));
if (a[y] >= 3 && fuck < sx) a[y] -= 3, dfs(x, y + 1, fuck + 1), a[y] += 3; if (a[x] >= 1 && a[y] >= 1) a[x] -= 1, a[y] -= 1, dfs(x, y + 1, fuck), a[x] += 1, a[y] += 1; if (a[x] >= 3 && fuck < sx) a[x] -= 3, dfs(x, y + 1, fuck + 1), a[x] += 3; return; }
#define E exit(0);
signed main() { n = read(); for (int i = 1; i <= n; ++ i) a[i] = read(), s += a[i];
sx = s - n * n + n; dfs(1, 2, 0);
put(res); return 0; }
|