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

 

全部评论

相关推荐

存一下准备挨个投一下
小小:银行秋招真题,欢迎大家来刷:牛客 -> 题库 -> 国央企笔试真题 。https://www.nowcoder.com/exam/company?questionJobId=195&subTabName=written_page
点赞 评论 收藏
分享
可爱的牛油果在求佛:再给你说一点,之前我的简历像流水账,当时我在面试的时候,面试官说:“你简历上的都是在调包吗?有自己的改进吗?如果没有改进直接调包的话,我觉得没什么可深挖的”。当时给我整懵了。其实大部分确实是在调包,因为我确实就用到这些简单的技术,如果只是把技术要点写在简历上,那没什么好说的,没意思,没什么深挖的。但是调包与调包之间仍存在区别,那就是自己的思考,如果你不把自己的困难摆出来,人家觉得就是简单的调包,有啥难的。其实只有你自己知道这个项目的难点在哪,只有你自己知道为什么要用这个技术,为什么要调这个包,而你需要展示的,不是技术,而是这个“为什么”,这是关键。所以,当你的技术不是很硬核的时候,就要突出自己的思考,这时候“思考”是难点,而当你的简历很硬核,技术很复杂时,技术本身就是难点。
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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