frp

SPOJ_423

frp
Aug 22nd, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. long long r[1 << 20][21];
  4. int m[20][20];
  5. int n;
  6. long long recurse(int i, int j)
  7. {
  8.     if(r[i][j] >= 0)return r[i][j];
  9.     r[i][j] = 0;
  10.     for(int l = 0; l < n; l++)
  11.         if(m[l][j - 1] && i & (1 << l))
  12.             r[i][j] += recurse(i ^ (1 << l),j - 1);
  13.     return r[i][j];
  14. }
  15. void test()
  16. {
  17.     cin >> n;
  18.     for(int i = 0; i < (1 << n); i++)
  19.         for(int j = 0; j <= n; j++)
  20.             r[i][j] = -1;
  21.     for(int i = 0; i < n; i++)
  22.         for(int j = 0; j < n; j++)
  23.             cin >> m[i][j];
  24.     r[0][0] = 1;
  25.     cout << recurse((1 << n) - 1,n) << '\n';
  26. }
  27. int main()
  28. {
  29.     int c;
  30.     cin >> c;
  31.     for(int i = 0; i < c; i ++)
  32.         test();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment