ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)
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(递推 && 状压)的更多相关文章
- ACM学习历程—HDU5396 Expression(递推 && 计数)
Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...
- ACM学习历程—HDU5418 Victor and World(动态规划 && 状压)
这个题目由于只有16个城市,很容易想到去用状压来保存状态. p[i][state]表示到i城市经过state状态的城市的最优值(state的二进制位每一位为1表示经过了该城市,否则没经过) 这样p[j ...
- ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- Problem Arrangement ZOJ - 3777(状压dp + 期望)
ZOJ - 3777 就是一个入门状压dp期望 dp[i][j] 当前状态为i,分数为j时的情况数然后看代码 有注释 #include <iostream> #include <cs ...
- uva10401Injured Queen Problem(递推)
题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...
- 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- ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- 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 ...
- ACM学习历程—HDU 5326 Work(树形递推)
Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...
随机推荐
- A/B测试与灰度发布
1.A/B测试与灰度发布的理论 产品是多维度的,设计体验.交互体验.系统质量.运营支持等等, 测试的目的是为了系统最终的交付,一套各方面都足够好的系统,而不是文档上定义的系统,系统是需要不断进化的. ...
- linux集群管理
本文以ubuntu-16.04.3-server-amd64为例,搭建服务器集群.同样是依托于虚拟机. 创建第一个节点 创建新的虚拟机参见:创建新的虚拟机,创建之后,编辑虚拟机,选择Ubuntu镜像, ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 6.1-AliasRegistry
AliasRegistry //AliasRegistry package org.springframework.core; /** * Common interface for managing ...
- bug-5——(js)indexOf()
indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置. ①对大小写敏感 ②如果要检索的字符串值没有出现,则该方法返回-1. ③位置时从0开始的 $j(this).html().ind ...
- CVPR 2018:diractNets网络,有残差网络好吗?
我把我明天讲PPT的材料弄上来了........哈 哈哈
- nginx 启动/停止/重启
启动: -c filename : set configuration file (default: conf/nginx.conf) [root@LinuxServer sbin]# /usr/ ...
- codeforces 60B bfs
题意:给出一个六面体分为k层,每层n行m列,每个小立方体有'.'(空)与'#'(障碍)的状态,第一层某个空的位置有一个水龙头,水流每次往六个方向流动(...).最少时间水流能把立方体空的部分填满. 思 ...
- castle windsor学习-----XML Inline Parameters 内联参数
当使用XML配置的时候,可能要给组件指定各种各样的依赖 1.简单的参数 参数名称不区分大小写 <component id="ping" type="Acme.Crm ...
- java学习进度条四