ACM学习历程—ZOJ 3777 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 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(递推 && 状压)的更多相关文章
- ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...
- ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)
Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- zoj 3777 Problem Arrangement(壮压+背包)
Problem Arrangement Time Limit: 2 Seconds Memory Limit: 65536 KB The 11th Zhejiang Provincial C ...
- zoj 3777 Problem Arrangement
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5264 题意:给出n道题目以及每一道题目不同时间做的兴趣值,让你求出所有做题顺序 ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- ACM学习历程—HDU1023 Train Problem II(递推 && 大数)
Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know ...
随机推荐
- 旋转卡壳求两个凸包最近距离poj3608
#include <iostream> #include <cmath> #include <vector> #include <string.h> # ...
- <转载> 为什么在Python里推荐使用多进程而不是多线程?
经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: ...
- (转)Java并发编程:阻塞队列
原文地址: http://www.cnblogs.com/dolphin0520/p/3932906.html 一.几种主要的阻塞队列 自从Java 1.5之后,在java.util.concurre ...
- 九度OJ刷题报告
从8月初到现在,已经刷了400道题,越到后面题目越难,但仍会继续努力. 现将自己所AC的代码贴到博客上整理,同时供大家交流参考. 所有代码均为本人独立完成,全部采用C语言进行编写.
- 【python】-- RabbitMQ 安装、基本示例、轮询机制
RabbitMQ MQ全称为Message Queue, 是一种分布式应用程序的的通信方法,它是消费-生产者模型的一个典型的代表,producer往消息队列中不断写入消息,而另一端consumer则可 ...
- Visual studio 创建文件时自动添加备注
Visual studio 创建文件时自动添加备注 描述 要求每回添加一个类,普通类或单元测试类文件头自动添加备注, 比如:Copyright, FileName, Author and so on. ...
- python基础14 ---函数模块4(configparser模块)
configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...
- STM32 ~ 串口DMA通道查找
STM32F4XX: /**************************************************************************************** ...
- elk示例-精简版
作者:Danbo 2016-03-09 1.Grok正则捕获 input {stdin{}} filter { grok { match => { "message" =&g ...
- 《程序员代码面试指南》第二章 链表问题 在单链表和双链表中删除倒数第K个节点
题目 在单链表和双链表中删除倒数第K个节点 java代码 /** * @Description:在单链表和双链表中删除倒数第K个节点 * @Author: lizhouwei * @CreateDat ...