Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <set>
- #include <cstring>
- using namespace std;
- int n;
- int mat[21][21];
- long long dp[1 << 20];
- long long rec(int red, int bitmask) {
- if(__builtin_popcount(bitmask) == n) {
- return 1;
- }
- if(dp[bitmask] != -1) {
- return dp[bitmask];
- }
- long long result = 0;
- for(int i = 0; i < n; i++) {
- if(mat[red][i] == 1) {
- if((bitmask & (1 << i)) == 0) {
- int new_mask = (bitmask | (1 << i));
- result += rec(red + 1, new_mask);
- }
- }
- }
- return dp[bitmask] = result;
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- int t;
- cin >> t;
- while(t--) {
- cin >> n;
- for(int i = 0; i < n; i++){
- for(int j =0 ; j < n; j++) {
- cin >> mat[i][j];
- }
- }
- memset(dp, -1, sizeof dp);
- cout << rec(0, 0) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement