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. ShowModal 代码分析

    下面为Delphi中,方法TCustomForm.ShowModal的代码,通过分析以下代码,可以了解ShowModal到底是怎么一回事! 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  2. vue前戏ES6

    es6语法 es6语法:let和const: { var a=123; let b=234; } console.log(a); console.log(b); 浏览器里会只看到123; 而且还会抱一 ...

  3. SpringMVC请求流程

    Spring结构图 SpringMVC请求流程图 SpringMVC请求流程图语述: request--->DispatcherServler(中央调度器/前端控制器)---> Handl ...

  4. python数据分析之:时间序列一

    在处理很多数据的时候,我们都要用到时间的概念.比如时间戳,固定时期或者时间间隔.pandas提供了一组标准的时间序列处理工具和数据算法. 在python中datetime.datetime模块是用的最 ...

  5. java_Ninja实战过程

    使用Ninja马上两年了,之前多多少少的都是跟着项目模仿着写,今年上半年准备从一个小项目开始从始至终走一遍; 首先官网:http://www.ninjaframework.org; github: h ...

  6. mysql 分页测试,

    大环境:MySQL5.6 自己造了 27万数据, 一次性 查出来,会超时: 而分页跑,会查出来8s: 但是在少于27万时,直接查比 分页查快5倍:

  7. Eclipse cdt mingw配置记录

    本人下载的是Eclipse C/C++ IDE for Neon.3,下载页面是:http://www.eclipse.org/cdt/downloads.php. 1. 运行eclipse后,在He ...

  8. jQuery图片水平滑动延迟加载动画

    在线演示 本地下载

  9. 比较分析与数组相关的sizeof和strlen

    首先,我们要清楚sizeof是C/C++中的一个操作符,其作用就是返回一个对象或者类型所占的内存字节数. 而,strlen是一个函数,函数原型为: size_t strlen(const char * ...

  10. my.cnf重要配置参数说明

    不同存储引擎中关键参数优化 MyISAM存储引擎 MyISAM存储引擎适用于读多写少,对读性能要求比较高的系统 官方文档:http://dev.mysql.com/doc/refman/5.6/en/ ...