dp[i][j],若i中有temp个1则表示为前temp个物品已选好位置,且他们的位置情况用i的二进制位表示.j表示在i情况下达到intersting value值为j是的选法个数。
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <queue>
#define maxn 40005
#define INF 1e17
using namespace std;
typedef long long ll;
ll dp[1<<13][505];
int num[13][13];
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
int main(){
// freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%d", &num[i][j]);
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for(int i = 0; i < (1<<n); i++){
int temp = 0;
for(int j = 0; j < n; j++){
if(i & (1<<j))
temp++;
}
for(int j = 0; j < n; j++){
if(i & (1<<j))
continue;
for(int h = 0; h <= m; h++)
if(h + num[temp][j] >= m)
dp[i+(1<<j)][m] += dp[i][h];
else
dp[i+(1<<j)][h+num[temp][j]] += dp[i][h];
}
}
ll p = 1;
for(int i = 1; i <= n; i++)
p *= i;
if(dp[(1<<n)-1][m] == 0)
cout << "No solution" << endl;
else{
ll d = gcd(dp[(1<<n)-1][m], p);
cout << p/d << "/" << dp[(1<<n)-1][m]/d << endl;
}
}
return 0;
}