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 addPij 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!种取法。也就是最多12! = 479001600这个复杂度太大。

但是这么多状态都是互异的,是不可能不计算的。

于是考虑状态能不能合并,考虑到我第一行取第一个,第二行取第三个这种情况,和第一行取第三个,第二行取第一个这种情况,都导致后面的行不能取1、3两列。

于是从第一行开始取,只考虑哪几列取过了。于是p[state][w]就表示取了state(二进制状压)的状态下,和为w的种数。

那么p[state|(1<<i)][w+a[cnt+1][i]] += p[state][w];

cnt表示当前取过几行,i表示那一列没有取过。

这样的话递推关系就能实现了。

最后要求大于等于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学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)的更多相关文章

  1. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  2. ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)

    Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...

  3. ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

    Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...

  4. ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds      Me ...

  5. zoj 3777 Problem Arrangement(壮压+背包)

    Problem Arrangement Time Limit: 2 Seconds      Memory Limit: 65536 KB The 11th Zhejiang Provincial C ...

  6. zoj 3777 Problem Arrangement

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5264 题意:给出n道题目以及每一道题目不同时间做的兴趣值,让你求出所有做题顺序 ...

  7. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  8. ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)

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

  9. ACM学习历程—HDU1023 Train Problem II(递推 && 大数)

    Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know  ...

随机推荐

  1. spring mvc中的@PathVariable

    spring mvc中的@PathVariable是用来获得请求url中的动态参数的,十分方便,复习下: @Controller public class TestController { @Requ ...

  2. python的协程和_IO操作

    协程Coroutine: 协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行. 注意,在一个子程序中中断,去执行其他子程序,不是函数调用,有点 ...

  3. Convex combination

    en.wikipedia.org/wiki/Convex_combination 凸组合 In convex geometry, a convex combination is a linear co ...

  4. 记一次Net软件逆向的过程(经典)

    查壳 1.先看下目录结构: 2.查下,是什么语言 ==> Net的,那不用说了,肯定能破解(毕竟是老本行嘛~) 混淆与反混淆 3.dnSpy打开后发现很多变量是乱码 4.用de4dot跑一波 5 ...

  5. Gradle-jar-aar

    Ref:Android Studio系列教程 Ref:Android Studio系列教程四--Gradle基础 Ref:Intellij IDEA 14.x 中的Facets和Artifacts的区 ...

  6. JavaScript螺旋矩阵

    螺旋矩阵                                                           螺旋矩阵指一个呈螺旋状的矩阵,其数字由第一行开始到右边不断变大,向下变大, ...

  7. HTTP基础概念讲解

    HTTP基础概念讲解 作者:Danbo 时间:2016-03-17 1.1.http协议头部:curl -I www.meituan.com 1.2.静态和动态 静态网页:纯HTML格式的网页,后台没 ...

  8. 事件监听机制——列出指定目录内容、添加Dialog对话框

    事件监听机制理解与Dialog练习 利用Java语言,仿照我的电脑目录进行打开目录,输入文件路径,查看该路径下所有的文件,设置两个文本框,一个转到按钮,当点击转到按钮时,查看路径是否正确,若正确在第二 ...

  9. finally return 执行关系 异常处理 c#

    Return.finally执行关系简述 除了函数出现system.exit(0)终止虚拟机,finally中的代码一定执行,return语句会等待finally的执行:如果是值传递,finally中 ...

  10. 从mysqldump整库备份文件中恢复单表

    最近,系统更新出现了问题,比较紧急,需要对三张表进行回档.由于我们都是采用mysqldump进行每天全备整库,数据量比较大,一个备份文件大概有70G,需要从这个70G文件中恢复三张表,真是蛋疼至极啊, ...