Description

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

题目大意是求每行每列取一个数,然后求和,求其和大于等于m的概率。

首先一行一行取下去,取法满足乘法运算,一共有n!种,n取12的话,12!=479001600,这么大取几遍肯定T了。

而且n!个状态是互不相异的状态,状态个数是减不下了的。

于是只能考虑如何合并一些状态。

按行取的话,我第一行取第一个,第二行取第三个,或者第一行取第三个,第二行取第一个,

这两种取法虽然不同,但是达到了一个效果,就是往后的行不能再取第一列和第三列。

于是我考虑把取过相同列的状态合并为一个状态。

于是考虑这样一个状态p[state][w]表示取到state状态,能取到和为w的取法数。

那么state为1的位表示取了那一列的数,为0表示没取。

且设当前取到了cnt行,cnt是state里面1的个数。

于是p[state|(1<<i)][w+a[cnt+1][i]] += p[state][w],表示由state状态更新state|(1<<i)状态,前提是state的第i位为0。

这样的话,本来后一个状态需要遍历前一个状态来求和,现在由前一个状态来更新后一个状态,每个状态便只需要遍历一次了。

这个状态的遍历此处采用了bfs。

bfs每个状态进队一次,然后遍历每一位需要O(n),遍历每个w需要O(m),

总复杂度是O(n*m(2^n)),最高是12*500*2^12=24576000,复杂度减了一个数量级。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; typedef pair<int, int> pii;
int n, m, a[][];
int p[(<<)+][], to, all;
bool vis[(<<)+]; void input()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i)
for (int j = ; j <= n; ++j)
scanf("%d", &a[i][j]);
memset(p, , sizeof(p));
memset(vis, false, sizeof(vis));
p[][] = ;
to = ;
all = ;
for (int i = ; i <= n; ++i)
{
to |= (<<i);
all *= i;
}
} //GCD
//求最大公约数
//O(logn)
int gcd(int a, int b)
{
if (b == )
return a;
else
return gcd(b, a%b);
} void bfs()
{
queue<pii> q;
q.push(pii(, ));
vis[] = true;
pii now;
int k, cnt;
while (!q.empty())
{
now = q.front();
q.pop();
k = now.first;
cnt = now.second;
vis[k] = false;
for (int i = ; i <= n; ++i)
{
if (k&(<<i))
continue;
for (int v = ; v <= m; ++v)
{
if (p[k][v] == )
continue;
p[k|(<<i)][v+a[cnt+][i]] += p[k][v];
if (!vis[k|(<<i)] && cnt+ != n)
{
q.push(pii(k|(<<i), cnt+));
vis[k|(<<i)] = true;
}
}
}
}
} void work()
{
bfs();
int ans = , d;
for (int i = ; i < m; ++i)
ans += p[to][i];
ans = all-ans;
d = gcd(all, ans);
if (ans == )
printf("No solution\n");
else
printf("%d/%d\n", all/d, ans/d);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}

ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)的更多相关文章

  1. ACM学习历程—HDU5396 Expression(递推 && 计数)

    Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...

  2. ACM学习历程—HDU5418 Victor and World(动态规划 && 状压)

    这个题目由于只有16个城市,很容易想到去用状压来保存状态. p[i][state]表示到i城市经过state状态的城市的最优值(state的二进制位每一位为1表示经过了该城市,否则没经过) 这样p[j ...

  3. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  4. Problem Arrangement ZOJ - 3777(状压dp + 期望)

    ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...

  5. uva10401Injured Queen Problem(递推)

    题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...

  6. ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)

    Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...

  7. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  8. ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)

    Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...

  9. ACM学习历程—HDU 5326 Work(树形递推)

    Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...

随机推荐

  1. spring+struts1

    概括及介绍: 集成原理:在Action 中获得BeanFactory,通过BeanFactory取得业务逻辑对象 本例采用:JDK1.8,tomcat7.0.9  技术点:spring与strut1集 ...

  2. 【原创】Hibernate自动生成(2)

    本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...

  3. 我的Android进阶之旅------>Android基于HTTP协议的多线程断点下载器的实现

    一.首先写这篇文章之前,要了解实现该Android多线程断点下载器的几个知识点 1.多线程下载的原理,如下图所示 注意:由于Android移动设备和PC机的处理器还是不能相比,所以开辟的子线程建议不要 ...

  4. emmet缩写格式

    2016年5月30日 10:10 标准网页<!DOCTYPE html> <html lang="zh-CN"> <head> <meta ...

  5. Java mail 发送邮件 主题(标题)乱码

    最近开发遇到Javamail  发送邮件标题乱码问题,腾讯.网易邮箱不会乱码,阿里邮箱  标题则会乱码.解决办法: String subject = MimeUtility.encodeWord(ma ...

  6. 中国移动OnetNet云平台 GET指令使用

    GET /devices//datastreams/KEY HTTP/1.1 Host: api.heclouds.com api-key: pmWLtnkDBSreKfvg7GsClUXdXa4A ...

  7. LeetCode:完全平方数【279】【DP】

    LeetCode:完全平方数[279][DP] 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示 ...

  8. afinal 文件上传、下载、图片加载实例

    // Afinal框架讲解 public class AfinalActivity extends FinalActivity { @ViewInject(id=R.id.bt_afinal_load ...

  9. 一张图学习vim常用命令

    一张图学习vim常用命令

  10. CSS3自定义下拉框菜单

    在线演示 本地下载