(中等) HDU 4979 A simple math problem. , DLX+重复覆盖+打表。
Description
Some people buy multiple accumulated lotteries to guarantee a higher possibility to get the top prize. Despite of this, it’s still not worthy to guarantee a top prize.Knowing this, Dragon changes his target to second tier prize. To get a second tier prize, you need to contain all of the R numbers with M numbers picked.Given N, M and R, Dragon wants to know how many M-accumulated lotteries he needs to buy, so that he can guarantee that he can get at least the second tier prize.
#include<iostream>
#include<cstring> using namespace std; const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM;
const int INF=10e8; struct DLX
{
int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode];
int H[MaxN],S[MaxM];
int ans;
int n,m,size; void init(int _n,int _m)
{
n=_n;
m=_m;
size=m;
ans=INF; for(int i=;i<=m;++i)
{
L[i]=i-;
R[i]=i+;
U[i]=D[i]=i; S[i]=;
}
L[]=m;
R[m]=; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
++S[c]; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[H[r]]=size;
}
} void remove(int c)
{
for(int i=D[c];i!=c;i=D[i])
{
L[R[i]]=L[i];
R[L[i]]=R[i];
}
} void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
L[R[i]]=R[L[i]]=i;
} bool vis1[MaxM]; int getH()
{
int ret=; for(int i=R[];i!=;i=R[i])
vis1[i]=; for(int i=R[];i!=;i=R[i])
if(vis1[i])
{
++ret;
vis1[i]=; for(int j=D[i];j!=i;j=D[j])
for(int k=R[j];k!=j;k=R[k])
vis1[col[k]]=; //!!!!!!!!!!!!!!
} return ret;
} void Dance(int d)
{
if(getH()+d>=ans)
return; if(R[]==)
{
if(d<ans)
ans=d; return;
} int c=R[]; for(int i=R[];i!=;i=R[i])
if(S[i]<S[c])
c=i; for(int i=D[c];i!=c;i=D[i])
{
remove(i); for(int j=R[i];j!=i;j=R[j])
remove(j); Dance(d+); for(int j=L[i];j!=i;j=L[j])
resume(j); resume(i);
}
}
}; DLX dlx;
int N,M,R;
int C[][]; void getC()
{
for(int i=;i<=;++i)
C[i][]=; for(int i=;i<=;++i)
for(int j=;j<=i;++j)
C[i][j]=C[i-][j-]+C[i-][j];
} void numTOp(int *s,int x,int n,int m,int d)
{
if(m<=)
return; if(x>=C[n-][m-])
numTOp(s,x-C[n-][m-],n-,m,d+);
else
{
s[]=d;
numTOp(s+,x,n-,m-,d+);
}
} int pTOnum(int *s1,int *s2)
{
int vis[],ans1=; memset(vis,,sizeof(vis)); for(int i=;i<R;++i)
vis[s1[s2[i]-]]=; int tR=R-; for(int i=;i<=N && tR>=;++i)
{
if(vis[i]==)
ans1+=C[N-i][tR];
else
--tR;
} return ans1;
} void slove()
{
int s[];
int ts[],temp; dlx.init(C[N][M],C[N][R]); for(int i=;i<C[N][M];++i)
{
numTOp(s,i,N,M,); for(int j=;j<C[M][R];++j)
{
numTOp(ts,j,M,R,); temp=pTOnum(s,ts); dlx.Link(i+,temp+);
}
} dlx.Dance(); cout<<dlx.ans<<endl;
} int main()
{
ios::sync_with_stdio(false); int T; cin>>T; getC(); for(int cas=;cas<=T;++cas)
{
cin>>N>>M>>R; cout<<"Case #"<<cas<<": ";
slove();
} return ;
}
把结果保存到文件里然后再复制到一个数组就行了。。。。。。
(中等) HDU 4979 A simple math problem. , DLX+重复覆盖+打表。的更多相关文章
- hdu - 4979 - A simple math problem.(可反复覆盖DLX + 打表)
题意:一种彩票共同拥有 N 个号码,每注包括 M 个号码,假设开出来的 M 个号码中与自己买的注有 R 个以上的同样号码,则中二等奖,问要保证中二等奖至少要买多少注(1<=R<=M< ...
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU 1757 A Simple Math Problem (矩阵乘法)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 5974 A Simple Math Problem
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...
- HDU 1757 A Simple Math Problem(矩阵高速幂)
题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...
随机推荐
- 转:SSH原理与运用(二):远程操作与端口转发
作者:阮一峰 (Image credit: Tony Narlock) 七.远程操作 SSH不仅可以用于远程主机登录,还可以直接在远程主机上执行操作. 上一节的操作,就是一个例子: $ ssh use ...
- Windsock套接字I/O模型学习 --- 第一章
1. I/O模型共有以下几种: 阻塞(blocking)模型 选择(select)模型 WSAAsyncSelect模型 WSAEventSelect模型 重叠(overlapped)模型 完成端口( ...
- js设置全局变量ajax中赋值
js设置全局变量,在ajax中给予赋值赋值不上问题解决方案 方案一. //在全局或某个需要的函数内设置Ajax异步为false,也就是同步. $.ajaxSetup({async : false}); ...
- POJ3264/RMQ
题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...
- nand驱动移植
首先下载nand flash驱动 s3c_nand.c ,此文件包含着nand flash驱动具体的实现,将其复制到drivers/mtd/nand下: s3c_nand.c 下载地址 s3c_nan ...
- BCDBOOT命令参数介绍
BCDboot 命令行选项 更新时间: 2013年10月 应用到: Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R ...
- JS中获取页面单选框radio和复选框checkbox中当前选中的值
单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...
- InnoDB的数据页结构
页是InnoDB存储引擎管理数据库的最小磁盘单位.页类型为B-tree node的页,存放的即是表中行的实际数据了. InnoDB数据页由以下七个部分组成,如图所示: File Header(文件头) ...
- AJAX(XMLHttpRequest)进行跨域请求方法详解(三)
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 3,带验证信息的请求 身份验证是Web开发中经常遇到的问题,在跨 ...
- chrome下如何显示打开网页的IP地址
Website IP:装上之后在网页右下角能够显示当前访问网页的IP地址,这个对定位哪台前端机是有问题的,特别有帮助.