zoj3777 Problem Arrangement(状压dp,思路赞)

The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward is going to arrange the order of the problems. As we know, the arrangement will have a great effect on the result of the contest. For example, it will take more time to finish the first problem if the easiest problem hides in the middle of the problem list.

There are N problems in the contest. Certainly, it's not interesting if the problems are sorted in the order of increasing difficulty. Edward decides to arrange the problems in a different way. After a careful study, he found out that the i-th problem placed in the j-th position will add Pij points of "interesting value" to the contest.

Edward wrote a program which can generate a random permutation of the problems. If the total interesting value of a permutation is larger than or equal to M points, the permutation is acceptable. Edward wants to know the expected times of generation needed to obtain the first acceptable permutation.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (1 <= N <= 12) and M (1 <= M <= 500).

The next N lines, each line contains N integers. The j-th integer in the i-th line is Pij (0 <= Pij <= 100).

Output

For each test case, output the expected times in the form of irreducible fraction. An irreducible fraction is a fraction in which the numerator and denominator are positive integers and have no other common divisors than 1. If it is impossible to get an acceptable permutation, output "No solution" instead.

Sample Input

2
3 10
2 4 1
3 2 2
4 5 3
2 6
1 3
2 4

Sample Output

3/1
No solution
题意:让你安排n个问题的顺序,第i个问题安排在第j个位置会有p[i][j]的价值,问安排后总价值大于等于m 的期望是多少。
思路:直接枚举会超时,发现n比较小,所以采用状压dp。用dp[i][state][j]表示当前正安排第i个问题,当前已经安排问题位置的状态为state,总价值为j的方案数。这里i这一维可以省略不写。
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
#define maxn 1005
#define inf 999999999
int a[20][20],dp[1<<13][505];
int jiecheng[20];
void init()
{
    int i,j;
    jiecheng[1]=1;
    for(i=2;i<=12;i++){
        jiecheng[i]=jiecheng[i-1]*i;
    }
}

int cal(int state)
{
    int i,j,tot=0;
    while(state){
        if(state&1)tot++;
        state>>=1;
    }
    return tot;
}

int gcd(int a,int b){
    return (b>0)?gcd(b,a%b):a;
}


int main()
{
    int n,m,i,j,T,state;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                scanf("%d",&a[i][j]);
            }
        }
        for(state=0;state<(1<<n);state++){
            for(j=0;j<=m;j++){
                dp[state][j]=0;
            }
        }
        dp[0][0]=1;
        for(state=1;state<(1<<n);state++){
            int tot=cal(state); //算出state中1的个数,即安排到第tot个问题
            for(i=1;i<=n;i++){
                if(state&(1<<(i-1))){
                    int state1=state^(1<<(i-1));
                    for(j=0;j<=m;j++){
                        int sum=j+a[tot][i];
                        if(sum>m)sum=m;
                        dp[state][sum]+=dp[state1][j];
                    }
                }
            }
        }
        int num1,num2;
        num1=dp[(1<<n)-1 ][m];
        if(num1==0){
            printf("No solution\n");continue;
        }
        num2=jiecheng[n];
        int gong=gcd(num1,num2);
        printf("%d/%d\n",num2/gong,num1/gong);
    }
    return 0;
}



你可能感兴趣的:(状压dp)