hdu - 4979 - A simple math problem.(可反复覆盖DLX + 打表)
题意:一种彩票共同拥有 N 个号码,每注包括 M 个号码,假设开出来的 M 个号码中与自己买的注有 R 个以上的同样号码,则中二等奖,问要保证中二等奖至少要买多少注(1<=R<=M<=N<=8)。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4979
——>>覆盖问题,yy可知是可反复覆盖问题,于是,DLX 上场。。
N个 选 R 个,共同拥有 C[N][R] 种选法,每种选法须要被覆盖,相应于 DLX 中的列。。
N个 选 M 个,共同拥有 C[N][M] 种选法,每种选法相应于 DLX 中的行。。
8 x 8 x 8 的大小,还能够打个表。。我的机子上打此表用时870s,约15分钟。。
所以。。不打表,測试数据又严谨的话,准过不了。。以前我想在比赛时开一个终端让程序打表比較长的时间,今天遇到了。。
核心:
#include <cstdio>
#include <cstring> const int MAXN = 8;
const int MAXR = 1000;
const int MAXC = 1000;
const int MAXNODE = MAXR * MAXC;
const int INF = 0x3f3f3f3f; int stateInCol[MAXC], ccnt;
int rcnt;
int bitcnt[1 << MAXN];
int C[MAXN + 1][MAXN + 1]; struct DLX
{
int sz;
int H[MAXR], S[MAXC];
int row[MAXNODE], col[MAXNODE];
int U[MAXNODE], D[MAXNODE], L[MAXNODE], R[MAXNODE];
int Min; void Init(int n)
{
for (int i = 0; i <= n; ++i)
{
U[i] = D[i] = i;
L[i] = i - 1;
R[i] = i + 1;
}
L[0] = n;
R[n] = 0; sz = n + 1;
memset(S, 0, sizeof(S));
memset(H, -1, sizeof(H));
} void Link(const int& r, const int& c)
{
row[sz] = r;
col[sz] = c;
D[sz] = D[c];
U[D[c]] = sz;
D[c] = sz;
U[sz] = c;
if (H[r] == -1)
{
H[r] = L[sz] = R[sz] = sz;
}
else
{
R[sz] = R[H[r]];
L[R[H[r]]] = sz;
R[H[r]] = sz;
L[sz] = H[r];
}
S[c]++;
sz++;
} void Remove(const int& c)
{
for (int i = D[c]; i != c; i = D[i])
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
} void Restore(const int& c)
{
for (int i = U[c]; i != c; i = U[i])
{
L[R[i]] = i;
R[L[i]] = i;
}
} int A()
{
int ret = 0;
bool vis[MAXC]; memset(vis, 0, sizeof(vis));
for (int i = R[0]; i != 0; i = R[i])
{
if (!vis[i])
{
vis[i] = true;
++ret;
for (int j = D[i]; j != i; j = D[j])
{
for (int k = R[j]; k != j; k = R[k])
{
vis[col[k]] = true;
}
}
}
} return ret;
} void Dfs(int cur)
{
if (cur + A() >= Min) return; if (R[0] == 0)
{
if (cur < Min)
{
Min = cur;
}
return;
} int c = R[0];
for (int i = R[0]; i != 0; 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);
}
Dfs(cur + 1);
for (int j = L[i]; j != i; j = L[j])
{
Restore(j);
}
Restore(i);
}
} int Solve()
{
Min = INF;
Dfs(0);
return Min;
} } dlx; int Bitcnt(int x)
{
int ret = 0; while (x)
{
ret += (x & 1);
x >>= 1;
} return ret;
} void GetBitcnt()
{
for (int i = 0; i < (1 << MAXN); ++i)
{
bitcnt[i] = Bitcnt(i);
}
} void GetC()
{
for (int i = 1; i <= MAXN; ++i)
{
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j)
{
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
}
}
} void Init()
{
GetBitcnt();
GetC();
} void Solve(int N, int M, int R)
{
ccnt = 0;
for (int i = 1; i < (1 << N); ++i)
{
if (bitcnt[i] == R)
{
stateInCol[i] = ++ccnt;
}
} dlx.Init(C[N][R]); rcnt = 0;
for (int i = 1; i < (1 << N); ++i)
{
if (bitcnt[i] == M)
{
++rcnt;
for (int j = i; j > 0; j = (i & (j - 1)))
{
if (bitcnt[j] == R)
{
dlx.Link(rcnt, stateInCol[j]);
}
}
}
} printf("%d", dlx.Solve());
} void SaveTable()
{
puts("{");
for (int N = 1; N <= MAXN; ++N)
{
puts(" {");
for (int M = 1; M <= N; ++M)
{
printf(" {");
for (int R = 1; R <= M; ++R)
{
if (R > 1)
{
printf(", ");
}
Solve(N, M, R);
}
printf("}");
if (M == N)
{
puts("");
}
else
{
puts(",");
}
}
printf(" }");
if (N == MAXN)
{
puts("");
}
else
{
puts(",");
}
}
puts("}");
} int main()
{
freopen("table.txt", "w", stdout); Init();
SaveTable(); return 0;
}
AC代码:
#include <cstdio> const int MAXN = 8; int ret[MAXN][MAXN][MAXN] = {
{
{1}
},
{
{2},
{1, 1}
},
{
{3},
{2, 3},
{1, 1, 1}
},
{
{4},
{2, 6},
{2, 3, 4},
{1, 1, 1, 1}
},
{
{5},
{3, 10},
{2, 4, 10},
{2, 3, 4, 5},
{1, 1, 1, 1, 1}
},
{
{6},
{3, 15},
{2, 6, 20},
{2, 3, 6, 15},
{2, 3, 4, 5, 6},
{1, 1, 1, 1, 1, 1}
},
{
{7},
{4, 21},
{3, 7, 35},
{2, 5, 12, 35},
{2, 3, 5, 9, 21},
{2, 3, 4, 5, 6, 7},
{1, 1, 1, 1, 1, 1, 1}
},
{
{8},
{4, 28},
{3, 11, 56},
{2, 6, 14, 70},
{2, 4, 8, 20, 56},
{2, 3, 4, 7, 12, 28},
{2, 3, 4, 5, 6, 7, 8},
{1, 1, 1, 1, 1, 1, 1, 1}
}
}; int main()
{
int T, N, M, R, kase = 0; scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &N, &M, &R);
printf("Case #%d: %d\n", ++kase, ret[N - 1][M - 1][R - 1]);
} return 0;
}
hdu - 4979 - A simple math problem.(可反复覆盖DLX + 打表)的更多相关文章
- (中等) HDU 4979 A simple math problem. , DLX+重复覆盖+打表。
Description Dragon loves lottery, he will try his luck every week. One day, the lottery company brin ...
- 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 ...
随机推荐
- opencv-形态处理
开运算 (Opening) 原理摘自:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/opening_closi ...
- Spring先进的交易管理困难剖析
1Spring事务传播行为 所谓事务传播行为就是多个事务方法相互调用时,事务怎样在这些方法间传播.Spring支持7种事务传播行为 PROPAGATION_REQUIRED(增加已有事务) 假设当前没 ...
- Java回合阵列List
package com.mine.practice.arrtolist; import java.util.ArrayList; import java.util.Arrays; import jav ...
- JavaScript面向对象旅程(下)
JavaScript面向对象旅程 剪不断,理还乱,是离愁. 前面已经提到过新语言开发的两个步骤,分别是:一.定义基本的数据类型,完善结构化编程语言的设计:二.为函数类型绑定this的概念,好在对象的方 ...
- linux中fork()函数具体解释(原创!!实例解说)
一.fork入门知识 一个进程,包含代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程差点儿全然同样的进程,也就是两个进程能够做全然同样的事,但假设初始參数或者传入的变量不 ...
- Spark编译与部署
Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建 [注] 1.该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取: 2.S ...
- C#从SQL server数据库中读取l图片和存入图片
原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...
- [TroubleShooting] The remote copy of database xx has not been rolled forward to a point in time
Steps: 1. backup database TestMirror on Pricipal server 2. backup database log of TestMirror on Pric ...
- jsp 行动标签
jsp行动标签 签.它影响JSP执行时的功能. 1.include动作标签 <jsp:include page="文件名称字"/> 或 <jsp:include ...
- JavaEE(3) - RMI服务器和客户端
1. 开发RMI服务器 Net Beans创建java project: (qs) (Server.java) package server; import java.rmi.*; //远程接口必须集 ...