zoj3777 Problem Arrangement(状压dp,思路赞)
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 的期望是多少。
思路:直接枚举会超时,发现n比较小,所以采用状压dp。用dp[i][state][j]表示当前正安排第i个问题,当前已经安排问题位置的状态为state,总价值为j的方案数。这里i这一维可以省略不写。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
#define maxn 1005
#define inf 999999999
int a[20][20],dp[1<<13][505];
int jiecheng[20];
void init()
{
int i,j;
jiecheng[1]=1;
for(i=2;i<=12;i++){
jiecheng[i]=jiecheng[i-1]*i;
}
}
int cal(int state)
{
int i,j,tot=0;
while(state){
if(state&1)tot++;
state>>=1;
}
return tot;
}
int gcd(int a,int b){
return (b>0)?gcd(b,a%b):a;
}
int main()
{
int n,m,i,j,T,state;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
for(state=0;state<(1<<n);state++){
for(j=0;j<=m;j++){
dp[state][j]=0;
}
}
dp[0][0]=1;
for(state=1;state<(1<<n);state++){
int tot=cal(state); //算出state中1的个数,即安排到第tot个问题
for(i=1;i<=n;i++){
if(state&(1<<(i-1))){
int state1=state^(1<<(i-1));
for(j=0;j<=m;j++){
int sum=j+a[tot][i];
if(sum>m)sum=m;
dp[state][sum]+=dp[state1][j];
}
}
}
}
int num1,num2;
num1=dp[(1<<n)-1 ][m];
if(num1==0){
printf("No solution\n");continue;
}
num2=jiecheng[n];
int gong=gcd(num1,num2);
printf("%d/%d\n",num2/gong,num1/gong);
}
return 0;
}
zoj3777 Problem Arrangement(状压dp,思路赞)的更多相关文章
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- ZOJ 3777 B - Problem Arrangement 状压DP
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...
- 2014 Super Training #4 B Problem Arrangement --状压DP
原题:ZOJ 3777 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...
- ZOJ 3777-Problem Arrangement(状压DP)
B - Problem Arrangement Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- [状压DP思路妙题]图
源自 luhong 大爷的 FJ 省冬令营模拟赛题 Statement 给定一个 \(n\) 个点 \(m\) 条边的图,没有重边与自环 每条边的两端点编号之差不超过 \(12\) 求选出一个非空点集 ...
- FZU2218 Simple String Problem(状压DP)
首先,定义S,表示前k个字符出现的集合,用二进制来压缩. 接下来,推出dp1[S],表示集合为S的子串的最长长度. 然后根据dp1[S]再推出dp2[S],表示集合为S或S的子集的子串的最长长度. 最 ...
- 「状压DP」「暴力搜索」排列perm
「状压DP」「暴力搜索」排列 题目描述: 题目描述 给一个数字串 s 和正整数 d, 统计 sss 有多少种不同的排列能被 d 整除(可以有前导 0).例如 123434 有 90 种排列能被 2 整 ...
- Problem Arrangement ZOJ - 3777(状压dp + 期望)
ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...
随机推荐
- Java虚拟机常用的性能监控工具
基础故障处理工具 jps: 虚拟机进程状况工具 功能:来处正在运行的虚拟机进程,并显示虚拟机执行主类名称,以及本地虚拟机唯一ID. 它是使用频率最高的命令行工具,因为其他JDK工具大多需要输入他查询到 ...
- 详解 TCP的三次握手四次挥手
本文转载来自https://blog.csdn.net/qzcsu/article/details/72861891 背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之 ...
- 十二:SQL注入之简要注入
SQL注入漏洞将是重点漏洞,分为数据库类型,提交方法,数据类型等方式.此类漏洞是WEB漏洞中的核心漏洞,学习如何的利用,挖掘,和修复是重要的. SQL注入的危害 SQL注入的原理 可控变量,带入数据库 ...
- mac配置Android SDK
下载地址:http://tools.android-studio.org/index.php/sdk 2.找到tools文件夹 选中android-sdk-macosx包下的tools文件夹,按com ...
- MySQL常用字符串函数和日期函数
数据函数 SELECT ABS(-8); /*绝对值*/ SELECT CEILING(9.4); /*向上取整*/ SELECT FLOOR(9.4); /*向下取整*/ SELECT RAND() ...
- 【LeetCode】数组排序题 Array_Sorts
数组排序 Array_Sorts LeetCode 数组排序题 88. 合并两个有序数组 合并两个有序数组 难度简单 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nu ...
- SDUST数据结构 - chap2 线性表
一.判断题: 二.选择题: 三.编程题: 7-1 jmu-ds-顺序表区间元素删除 : 输入样例: 10 5 1 9 10 67 12 8 33 6 2 3 10 输出样例: 1 67 12 33 2 ...
- SAP RFC的相关的术语说明
工作比较忙,很少有时间写点文章,抽空写点吧,给需要的人看看,虽然徒弟很多了,不过还是不要固步自封,在这里也指导更多的人进步吧. RFC(Remote Function Call)是SAP系统和其他(S ...
- postman接口测试之复制多个接口或collections到某个子文件夹或collections下
一.痛点 1.postman只支持复制一个请求,或者一个子文件夹,但是不支持复制多个请求,或者整个collections到某个子文件夹或者某个collections下. 2.网上查了好一会儿,没有一个 ...
- 2V升3V芯片,输入2V输出3V可达1A
PW5328B是一个恒定频率, 6引脚 SOT23电流模式升压转换器,用于小型低功耗应用. PW5328B的开关频率为 1.2MHz,允许使用微小的.低成本的电容器和电感器.内部软启动导致小涌流和延长 ...