ZOJ - 3777 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 Tindicating 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

        用dp[i][j]来代表状态位i时,价值为j的放置的数量有多少种,这样很容易便可得到转移方程组。

 

#include <bits/stdc++.h>
using namespace std;
const int maxn = 13;
const int maxm = 1500;
int dp[(1 << maxn)][maxm];
int val[maxn][maxn];
int sta[maxn], sum[(1 << maxn)];
int n, m;
void init() {
	sta[0] = 1;
	for (int i = 1; i < 13; i++)
		sta[i] = sta[i - 1] * 2;
	sum[0] = 0;
	for (int i = 1; i < (1 << 13); i++) {
		sum[i] = sum[(i&(i - 1))] + 1;
	}
}
int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}
int hh(int x) {
	int ans = 1;
	for (int i = 1; i <= x; ++i) {
		ans *= i;
	}
	return ans;
}
int main() {
	init();
	ios::sync_with_stdio(0);
	int te; cin >> te;
	while (te--) {
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				cin >> val[i][j];
		int big = (1 << n);
		for (int i = 0; i < big; i++) {
			for (int j = 0; j < n; j++) {
				int v = sta[j];
				if (i & v)continue;//
				int t = val[j][sum[i]];
				for (int k = 0; k <= m; k++) {
					if (k + t >= m)
						dp[(i | v)][m] += dp[i][k];
					else
						dp[(i | v)][k + t] += dp[i][k];
				}
			}
		}
		int ans = 0;
		ans += dp[big - 1][m];
		if (ans == 0) {
			cout << "No solution" << endl;
			continue;
		}
		int *** = hh(n);
		int g = gcd(***, ans);
		*** /= g;
		ans /= g;
		cout << *** << '/' << ans << '\n';
	}
	return 0;
}

 

全部评论

相关推荐

斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
一个菜鸡罢了:哥们,感觉你的简历还是有点问题的,我提几点建议,看看能不能提供一点帮助 1. ”新余学院“别加粗,课程不清楚是否有必要写,感觉版面不如拿来写一下做过的事情,教育经历是你的弱势就尽量少写 2. “干部及社团经历”和“自我评价”删掉 3. 论文后面的“录用”和“小修”啥的都删掉,默认全录用,问了再说,反正小修毕业前肯定能发出来 4. 工作经验和研究成果没有体现你的个人贡献,着重包装一下个人贡献
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务