zoj 3777 Problem Arrangement 【状压dp】

来源:互联网 发布:mac 拷贝隐藏文件 编辑:程序博客网 时间:2024/05/22 09:06

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

23 102 4 13 2 24 5 32 61 32 4

Sample Output

3/1No solution


无语了,先MLE,再TLE,AC好艰难。


题意:有n份工作和n个人,已知第i个人做第j份工作得到val[i][j]的乐趣。现在让你找到一种方案使得乐趣值大于或等于m,问你该方案的概率,写成最简分数。每个工作必须有且仅有一个人做。


思路:用dp[i][S][k]表示前i个人在状态S下获得k乐趣值的方案数。

dp[i][S|(1<<j)][min(k+val, m)] += dp[i-1][S][k],j表示i的位置,这样找到合法的状态S,然后暴力转移即可。

没想到先来了一发MLE,用了滚动数组,又TLE。。。最后发现转移时不加i的限制直接搞也可以,试了发终于过了。

假设S状态下有i个人,dp[S][k]表示前i个人在状态S下获得乐趣值k的方案数 

dp[S|(1<<j)][min(k+val, m)] += dp[S][k]。


AC代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (200+10)#define MAXM (500000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)using namespace std;int val[15][15];int dp[1<<12][501];int fac[15], f[15];int n, m;void get(){    fac[0] = f[0] = 1;    for(int i = 1; i <= 12; i++)        fac[i] = fac[i-1] * i, f[i] = f[i-1] * 2;}int gcd(int a, int b){    return b == 0 ? a : gcd(b, a%b);}int main(){    get();    int t; Ri(t);    W(t)    {        Ri(n); Ri(m);        for(int i = 0; i < n; i++)            for(int j = 0; j < n; j++)                Ri(val[i][j]);        int State = f[n]-1;        int sum = fac[n]; CLR(dp, 0);        dp[0][0] = 1;        for(int S = 0; S <= State; S++)        {            int now = -1;            for(int i = 0; i < n; i++) now += f[i] & S ? 1 : 0;            for(int i = 0; i < n; i++)            {                if(f[i] & S) continue;                int newS = f[i] | S;                for(int j = 0; j <= m; j++)                    dp[newS][min(j+val[now+1][i], m)] += dp[S][j];            }        }        int ans = dp[State][m];        if(ans == 0)            printf("No solution\n");        else        {            int GCD = gcd(ans, sum);            printf("%d/%d\n", sum / GCD, ans / GCD);        }    }    return 0;}


0 0