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;
}

 

全部评论

相关推荐

2025-12-28 16:32
重庆邮电大学 Java
程序员花海:1.技能放最后,来面试默认你都会,技能没啥用 2.实习写的看起来没啥含金量,多读读部门文档,包装下 接LLM这个没含金量 也不要用重构这种 不会给实习生做的 3.抽奖这个还是Demo项目,实际在公司里面要考虑策略,满减,触发点,触发规则 库存 之类的,不是这个项目这么简单 4.教育背景提前,格式为 教育背景 实习 项目 技能 自我评价
简历被挂麻了,求建议
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务