zoj 3777 Problem Arrangement

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
// 这题用到了状态压缩,在计算 第 i 行时 我们需要用到 前 i-1行 取好之后的状态 
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std ;

int qe[5010][510] ,len[5010] ;
vector<int>p[2] ;
int a[13][13] ,top ;
bool vi[5010] ;
LL get_n( int n )
{
	LL ans = 1 ;
	for( int i = 2 ; i <= n ;i++ )
		ans *= i ;
	return ans ;
}
LL gcd( LL a , LL b )
{
	if(b==0) return a ;
	return gcd(b,a%b) ;
}
int main()
{
	int sum , v ,sum2 , u ;
	int n , m, k, i ,T, j,ll ;
	int aa ,hehe ;
	//cout << get_n(12) << endl;
	LL ans ;
	cin >> T ;
	while(T--)
	{
		cin >> n >> m ;
		top = 0 ;
		for( i = 0 ; i < n ;i++ )
			for( j = 0 ; j < n ;j++ )
				scanf("%d",&a[i][j]) ;
		p[0].clear();p[1].clear() ;

        memset(qe,0,sizeof(qe)) ;

		ans = 0 ;
		for( i = 0 ; i < n ;i++ )
		{
			k = (1<<i) ;
			p[0].push_back(k) ;
			if(a[0][i] < m )
			{
				qe[k][a[0][i]]++ ;
			}
			else ans += get_n(n-1) ;
		}
		v = 1 ;
        // p[v] 放置前 i-1行取好之后的状态
        // p[1-v] 就是  前 i 行取好之后的状态
        // qe[i][u] 表示在 i 状态下 能取到 和为 u 的取法有多少种
        // 如果放了 a[i][j] 发现 >= m 则 在这个状态下放都是符合的 
        // 直接 ans += num*(n-i-1)! 就好了
		for( i = 1 ; i < n ;i++ ){
             v ^= 1 ;
			 	p[1-v].clear();
				memset(vi,0,sizeof(vi)) ;
			for( j = 0 ; j < n ;j++ ) 
			{ 
				for( k = 0 ; k < p[v].size() ;k++ )if((p[v][k]&(1<<j)) == 0 )// 表示可以在这个状态下放
				{ 
					 hehe = p[v][k] ;
                     for( u = 0 ; u < m ;u++ )if(qe[hehe][u])
					 {
						 aa = u+a[i][j] ;
						 if(aa >= m )
						 {
							 ans += qe[hehe][u]*get_n(n-i-1) ;
						 }
						 else 
						 {
                             ll = (p[v][k]|(1<<j)) ;
							 if(!vi[ll])
							 {
								 p[1-v].push_back(ll) ;
								 vi[ll] = 1 ;
								 // 标记前 i 行能达到的状态 保证不会有重负
							 }
							 qe[ll][aa] += qe[hehe][u] ;
						 }
					 }
				}
			}
		}

		if(ans==0)
		{
			puts("No solution") ;
			continue ;
		}
		LL tt = get_n(n) ;
		LL yy = gcd(tt,ans) ;
		cout << tt/yy << "/" << ans/yy << endl ;
	}
	return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值