Wise Men (Hard Version)

This is the hard version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved.

$n$ wise men live in a beautiful city. Some of them know each other.

For each of the $n!$ possible permutations $p_1, p_2, \ldots, p_n$ of the wise men, let’s generate a binary string of length $n-1$: for each $1 \leq i < n$ set $s_i=1$ if $p_i$ and $p_{i+1}$ know each other, and $s_i=0$ otherwise.

For all possible $2^{n-1}$ binary strings, find the number of permutations that produce this binary string.Input

The first line of input contains one integer $n$ ($2 \leq n \leq 18)$  — the number of wise men in the city.

The next $n$ lines contain a binary string of length $n$ each, such that the $j$-th character of the $i$-th string is equal to ‘1’ if wise man $i$ knows wise man $j$, and equals ‘0’ otherwise.

It is guaranteed that if the $i$-th man knows the $j$-th man, then the $j$-th man knows $i$-th man and no man knows himself.Output

Print $2^{n-1}$ space-separated integers. For each $0 \leq x < 2^{n-1}$:

  • Let’s consider a string $s$ of length $n-1$, such that $s_i = \lfloor \frac{x}{2^{i-1}} \rfloor \bmod 2$ for all $1 \leq i \leq n – 1$.
  • The $(x+1)$-th number should be equal to the required answer for $s$.

Examplesinput

3
011
101
110

output

0 0 0 6 

input

4
0101
1000
0001
1010

output

2 2 6 2 2 6 2 2 

Note

In the first test, each wise man knows each other, so every permutation will produce the string $11$.

In the second test:

  • If $p = \{1, 2, 3, 4\}$, the produced string is $101$, because wise men $1$ and $2$ know each other, $2$ and $3$ don’t know each other, and $3$ and $4$ know each other;
  • If $p = \{4, 1, 2, 3\}$, the produced string is $110$, because wise men $1$ and $4$ know each other, $1$ and $2$ know each other and $2$, and $3$ don’t know each other;
  • If $p = \{1, 3, 2, 4\}$, the produced string is $000$, because wise men $1$ and $3$ don’t know each other, $3$ and $2$ don’t know each other, and $2$ and $4$ don’t know each other.

Solution:

#undef _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
return '"' + s + '"';
}

string to_string(const char* s) {
return to_string((string) s);
}

string to_string(bool b) {
return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}

template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
}
return res;
}

template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

const int N = 18;

long long dp[1 << N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
vector<int> mask(n);
for (int i = 0; i < n; i++) {
    string foo;
    cin >> foo;
for (int j = 0; j < n; j++) {
      if (foo[j] == '1') {
        mask[i] |= (1 << j);
      }
    }
  }
  vector<vector<int>> ts(n + 1);
for (int t = 0; t < (1 << n); t++) {
    ts[__builtin_popcount(t)].push_back(t);
  }
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    dp[1 << i][i] = 1;
  }
  vector<long long> ret(1 << (n - 1));
  vector<int> part(1, 1);
function<void(int)> Dfs = [&](int r) {
if (r == n) {
long long res = 0;
for (int i = 0; i < n; i++) {
        res += dp[(1 << n) - 1][i];
      }
      debug(part, res);
      vector<int> a = part;
sort(a.begin(), a.end());
do {
int mask = 0;
for (int x : a) {
mask <<= 1;
          for (int i = 0; i < x - 1; i++) {
            mask = (mask << 1) | 1;
          }
        }
        ret[mask] += res;
      } while (next_permutation(a.begin(), a.end()));
      return;
    }
    if (part.size() == 1 || part.back() < part[part.size() - 2]) {
      part.back() += 1;
      for (int t : ts[r]) {
        int v = t;
        while (v > 0) {
int i = __builtin_ctz(v);
int u = ((1 << n) - 1 - t) & mask[i];
          while (u > 0) {
int j = __builtin_ctz(u);
dp[t | (1 << j)][j] += dp[t][i];
            u &= u - 1;
          }
          v &= v - 1;
        }
      }
      Dfs(r + 1);
      for (int t : ts[r + 1]) {
        int v = t;
        while (v > 0) {
int i = __builtin_ctz(v);
dp[t][i] = 0;
v &= v - 1;
}
}
part.back() -= 1;
}
{
part.push_back(1);
for (int t : ts[r]) {
long long val = 0;
for (int i = 0; i < n; i++) {
          val += dp[t][i];
        }
        {
          int u = ((1 << n) - 1 - t);
          while (u > 0) {
int j = __builtin_ctz(u);
dp[t | (1 << j)][j] += val;
            u &= u - 1;
          }
        }
      }
      Dfs(r + 1);
      for (int t : ts[r + 1]) {
        int v = t;
        while (v > 0) {
int i = __builtin_ctz(v);
dp[t][i] = 0;
v &= v - 1;
}
}
part.pop_back();
}
};
Dfs(1);
for (int i = 0; i < n - 1; i++) {
    for (int t = (1 << (n - 1)) - 1; t >= 0; t--) {
if (!(t & (1 << i))) {
        ret[t] -= ret[t | (1 << i)];
      }
    }
  }
  for (int t = 0; t < (1 << (n - 1)); t++) {
    if (t > 0) {
cout << " ";
    }
    cout << ret[t];
  }
  cout << '\n';
  debug(clock());
  return 0;
}