zoj 3777 Problem Arrangement (好状压dp)

Problem Arrangement Time Limit: 2 Seconds      Memory Limit: 65536 KB

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和m,接下来一个n*n的矩阵,a[i][j]表示第i道题放在第j个顺序做可以加a[i][j]的分数,问做完n道题所得分数大于等于m所需的期望(次数)。

思路:
设满足条件的方案数为s,总的方案数位n!,那么期望就是n!/s了。主要任务就是求方案数。
dp[k][i][j]-放完前k个,状态为i,获得的分数为j的方案数。
dp[k][i][j]-推出相应的dp[k+1][s][w]即可。
第一维的数组可以不用要,有四重循环了,复杂度有点高,加了一些小优化过的。
ps:可以看推荐博客,复杂度要小一点,三重循环。状态i有多少个1代表前几个人已经选了。

感想:
以前以为这种题不能用二进制状压的,学习了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 100005
#define OO (1<<31)-1
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans,tot;
int a[15][15],fac[13];
int dp[1<<12][505];

int gcd(int x,int y)
{
    return y==0?x:gcd(y,x%y);
}
int isok(int i)
{
    int t=0;
    while(i)
    {
        if(i&1) t++;
        i>>=1;
    }
    return t;
}
void solve()
{
    int i,j,t,k,p,s,w;
    tot=(1<<n)-1;
    for(i=0; i<=tot; i++) for(j=0; j<=m; j++) dp[i][j]=0;
    dp[0][0]=1;
    for(k=0; k<n; k++)
    {
        if(k==0)
        {
            i=j=0;
            for(p=0; p<n; p++)
            {
                if(i&(1<<p)) continue ;
                s=i|(1<<p);
                w=min(m,j+a[k+1][p+1]);
                dp[s][w]+=dp[i][j];
            }
            continue ;
        }
        for(i=tot; i>=0; i--)
        {
            if(isok(i)!=k) continue ;
            for(j=0; j<=m; j++)
            {
                if(!dp[i][j]) continue ;
                for(p=0; p<n; p++)
                {
                    if(i&(1<<p)) continue ;
                    s=i|(1<<p);
                    w=min(m,j+a[k+1][p+1]);
                    dp[s][w]+=dp[i][j];
                }
            }
        }
    }
    ans=dp[tot][m];
    if(ans==0) printf("No solution\n");
    else
    {
        int gg=gcd(ans,fac[n]);
        printf("%d/%d\n",fac[n]/gg,ans/gg);
    }
}
int main()
{
    int i,j,t;
    fac[0]=1;
    for(i=1; i<=12; i++)
    {
        fac[i]=fac[i-1]*i;
    }
    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]);
            }
        }
        solve();
    }
    return 0;
}









你可能感兴趣的:(zoj 3777 Problem Arrangement (好状压dp))