zoj3777 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 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 题目大意:给出n个物品,将他们排列,第i个物品放在j位置可获得p[i][j]的价值,求总排列数除以总价值大于m的排列数,结果以最简分数的形式输出。 思路: 递推题目,f[i][j][k]表示按顺序取,当前取到第i个物品时,状态为j,总价值为j的时候的方案数。递推方程,f[i+1][j+(2<<(t-1))][k+a[i,t]]+=f[i][j][k]。
/*
* Author: Joshua
* Created Time: 2014/5/17 14:31:42
* File Name: b.cpp
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
#include<utility>
#define M0(x) memset(x, 0, sizeof(x))
#define MP make_pair
#define Fi first
#define Se second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define Inf 0x3fffffff
#define eps 1e-8 #define b(i) (1<<i)
typedef long long LL;
using namespace std; int f[][b()][],p[];
int a[][];
int n,m; int gcd(int aa,int bb)
{
if (bb==) return aa;
else return gcd(bb,aa%bb);
} int main()
{
int tt;
int cc;
scanf("%d",&tt);
while (tt>)
{
tt--;
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
scanf("%d",&a[i][j]);
M0(f);
int temp;
int gg,vv;
f[][][]=;
gg=;
vv=;
for (int i=;i<n;i++)
{
gg=-gg;
vv=-vv;
M0(f[gg]);
for (int j=;j<=(<<n)-;j++)
{
cc=;
for (int t=;t<=n;t++)
if ((j & b(t-)) > ) cc++;
if (cc!=i) continue;
for (int t=;t<=n;t++)
if ((b(t-) & j)==)
for (int k=;k<=m;k++)
if (f[vv][j][k]>)
{
temp=k+a[i+][t];
if (temp>m) temp=m;
f[gg][ j|b(t-) ][temp]+=f[vv][j][k];
}
}
}
int sum=f[gg][b(n)-][m];
if (sum>)
{
int ss=;
for (int i=;i<=n;i++)
ss*=i;
int gc=gcd(sum,ss);
printf("%d/%d\n",ss/gc,sum/gc);
}
else
{
printf("No solution\n");
}
}
return ;
}
因为空间不够所以采用滚动数组,然后超时了所以要尽量把无效状态判掉。时间看起来是(2^(2n)*m*n),判无效状态后为(2^n*m*n)。其实我这是正着推,看同学反着推好像更好写且不用滚动数组和判无效,果然我还是写得太丑了。
zoj3777 Problem Arrangement的更多相关文章
- ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- zoj3777 Problem Arrangement(状压dp,思路赞)
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...
- B - Problem Arrangement ZOJ - 3777
Problem Arrangement ZOJ - 3777 题目大意:有n道题,第i道题第j个做可以获得Pij的兴趣值,问至少得到m兴趣值的数学期望是多少,如果没有的话就输出No solution. ...
- zoj 3777 Problem Arrangement(壮压+背包)
Problem Arrangement Time Limit: 2 Seconds Memory Limit: 65536 KB The 11th Zhejiang Provincial C ...
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- 2014 Super Training #4 B Problem Arrangement --状压DP
原题:ZOJ 3777 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...
- zoj 3777 Problem Arrangement
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5264 题意:给出n道题目以及每一道题目不同时间做的兴趣值,让你求出所有做题顺序 ...
- ZOJ 3777 B - Problem Arrangement 状压DP
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...
随机推荐
- linux下查看jdk路径
jdk安装后 centos中: 执行 rpm -ql java-1.7.0-openjdk-devel | grep '/bin/javac' 命令确定, 执行后会输出一个路径,除去路径末尾的 &qu ...
- MyBatis+mysql 简单分页
注意:limit不能跟动态内容 <select id="fenYe" parameterType="int" resultType="com.x ...
- HDU - 3697 Selecting courses
题目链接:https://vjudge.net/problem/HDU-3697 题目大意:选课,给出每门课可以的选课时间.自开始选课开始每过五分钟可以选一门课,开始 时间必须小于等于四,问最多可以选 ...
- 【nodejs】nodejs 的linux安装(转)
(一) 编译好的文件 简单说就是解压后,在bin文件夹中已经存在node以及npm,如果你进入到对应文件的中执行命令行一点问题都没有,不过不是全局的,所以将这个设置为全局就好了. ./node -v ...
- python cookbook第三版学习笔记十三:类和对象(三)描述器
__get__以及__set__:假设T是一个类,t是他的实例,d是它的一个描述器属性.读取属性的时候T.d返回的是d.__get__(None,T),t.d返回的是d.__get__(t,T).说法 ...
- 一步一步学习Vue(十一)
本篇继续学习vuex,还是以实例为主:我们以一步一步学Vue(四)中讲述的例子为基础,对其改造,基于vuex重构一遍,这是原始的代码: todolist.js ; (function () { var ...
- nodeJS之crypto加密
前面的话 加密模块提供了 HTTP 或 HTTPS 连接过程中封装安全凭证的方法.也提供了 OpenSSL 的哈希,hmac, 加密(cipher), 解密(decipher), 签名(sign) 和 ...
- Redux源码分析之bindActionCreators
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- 表达式求值(二叉树方法/C++语言描述)(五)
本例中的二叉树图是使用Graphviz绘制的(Graphviz官网),在Ubuntu Linux下可以使用apt-get命令安装它: sudo apt-get install graphviz 表达式 ...
- python几种常见语法
NO1.type自动匹配类型 name="zhangsan"age=18 print (type(name))#type自动匹配类型 print ("name:%s,ag ...