zoj3777 Problem Arrangement

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的排列数,结果以最简分数的形式输出。

思路: 递推题目,f[i][j][k]表示按顺序取,当前取到第i个物品时,状态为j,总价值为j的时候的方案数。递推方程,f[i+1][j+(2<<(t-1))][k+a[i,t]]+=f[i][j][k]。
  1 /*
  2  * Author:  Joshua
  3  * Created Time:  2014/5/17 14:31:42
  4  * File Name: b.cpp
  5  */
  6 #include<cstdio>
  7 #include<iostream>
  8 #include<cstring>
  9 #include<cstdlib>
 10 #include<cmath>
 11 #include<algorithm>
 12 #include<string>
 13 #include<map>
 14 #include<set>
 15 #include<vector>
 16 #include<queue>
 17 #include<stack>
 18 #include<ctime>
 19 #include<utility>
 20 #define M0(x) memset(x, 0, sizeof(x))
 21 #define MP make_pair
 22 #define Fi first
 23 #define Se second
 24 #define rep(i, a, b) for (int i = (a); i <= (b); ++i)
 25 #define red(i, a, b) for (int i = (a); i >= (b); --i)
 26 #define PB push_back
 27 #define Inf 0x3fffffff
 28 #define eps 1e-8
 29 
 30 #define b(i)  (1<<i)
 31 typedef long long LL;
 32 using namespace std;
 33 
 34 int f[2][b(12)][501],p[15];
 35 int a[13][13];
 36 int n,m;
 37 
 38 int gcd(int aa,int bb)
 39 {
 40     if (bb==0) return aa;
 41     else return gcd(bb,aa%bb);
 42 }
 43 
 44 
 45 int main()
 46 {
 47     int tt;
 48     int cc;
 49     scanf("%d",&tt);
 50     while (tt>0)
 51     {
 52         tt--;
 53         scanf("%d%d",&n,&m);
 54         for (int i=1;i<=n;i++)
 55             for (int j=1;j<=n;j++)
 56                 scanf("%d",&a[i][j]);
 57         M0(f);
 58         int temp;
 59         int gg,vv;
 60         f[0][0][0]=1;
 61         gg=0;
 62         vv=1;
 63         for (int i=0;i<n;i++)
 64         {
 65           gg=1-gg;
 66           vv=1-vv;
 67           M0(f[gg]);
 68           for (int j=0;j<=(1<<n)-1;j++)
 69           {
 70              cc=0;
 71              for (int t=1;t<=n;t++)
 72                if  ((j & b(t-1)) > 0) cc++;
 73              if  (cc!=i) continue;
 74              for (int t=1;t<=n;t++)
 75                 if ((b(t-1) & j)==0)
 76                   for (int k=0;k<=m;k++)
 77                   if (f[vv][j][k]>0)
 78                     {
 79                         temp=k+a[i+1][t];
 80                         if (temp>m) temp=m;
 81                         f[gg][ j|b(t-1) ][temp]+=f[vv][j][k];
 82                     }
 83           }
 84         }
 85         int sum=f[gg][b(n)-1][m];
 86         if (sum>0)
 87         {
 88             int ss=1;
 89             for (int i=1;i<=n;i++)
 90                 ss*=i;
 91             int gc=gcd(sum,ss);
 92             printf("%d/%d\n",ss/gc,sum/gc);
 93         }
 94         else
 95         {
 96             printf("No solution\n");
 97         }
 98     }
 99     return 0;
100 }

 

因为空间不够所以采用滚动数组,然后超时了所以要尽量把无效状态判掉。时间看起来是(2^(2n)*m*n),判无效状态后为(2^n*m*n)。其实我这是正着推,看同学反着推好像更好写且不用滚动数组和判无效,果然我还是写得太丑了。

posted @ 2014-05-18 14:37  一个大叔  阅读(552)  评论(0编辑  收藏  举报