hdu5803
hdu5803
题意
给出四个整数 A B C D,问有多少个四元组 (a, b, c, d) 使 a + c > b + d 且 a + d >= b + c ,0 <= a <= A ,0 <= b <= B,0 <= c <= C,0 <= d <= D .
分析
可以用数位dp解决这个问题。
同时对四个数进行数位dp,采用记忆化搜索的形式,搜索过程中考虑剪枝,考虑到百位数时,如果 a + c - b - d >= 2,那么到十位数时(a + c - b - d 最少也才 -18,而前面到十位数会乘 10 即得到 20)一定满足条件了(加上 a + d - b - c 同理),而 a + c - b - d <= -2 那么到十位数时无论如何都不会满足条件了,可以直接剪枝。
但是每次要有 10 * 10 * 10 * 10 的状态转移,还要考虑优化,将四个数转化成二进制数,再进行 dp,前面的剪枝仍然可用。
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int bit[5][65];
ll dp[62][5][5][16];
ll dfs(int l, int acbd, int adbc, int limit)
{
if(l < 0) return acbd > 0 && adbc >= 0;
ll& res = dp[l][acbd + 2][adbc + 2][limit];
if(res != -1) return res;
res = 0;
int up[4];
for(int i = 0; i < 4; i++)
{
up[i] = (limit >> i & 1) ? bit[i][l] : 1;
}
for(int a = 0; a <= up[0]; a++)
{
for(int b = 0; b <= up[1]; b++)
{
for(int c = 0; c <= up[2]; c++)
{
for(int d = 0; d <= up[3]; d++)
{
int acbd_ = acbd, adbc_ = adbc, limit_ = 0;
acbd_ = min(acbd_ * 2 + a + c - b - d, 2);
adbc_ = min(adbc_ * 2 + a + d - b - c, 2);
if(acbd_ <= -2 || adbc_ <= -2) continue;
if(a == up[0] && (limit & 1)) limit_ |= 1;
if(b == up[1] && (limit >> 1 & 1)) limit_ |= 2;
if(c == up[2] && (limit >> 2 & 1)) limit_ |= 4;
if(d == up[3] && (limit >> 3 & 1)) limit_ |= 8;
(res += dfs(l - 1, acbd_, adbc_, limit_)) %= MOD;
}
}
}
}
return res;
}
int main()
{
int T;
for(scanf("%d", &T); T--;)
{
memset(bit, 0, sizeof bit);
memset(dp, -1, sizeof dp);
ll A, B, C, D;
scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
for(int i = 0; i < 62; i++)
{
bit[0][i] = A >> i & 1;
bit[1][i] = B >> i & 1;
bit[2][i] = C >> i & 1;
bit[3][i] = D >> i & 1;
}
printf("%lld\n", dfs(60, 0, 0, 15));
}
return 0;
}
hdu5803的更多相关文章
随机推荐
- struts2 之 struts2数据处理
开门见山,struts2的数据处理总结: 1. 在servlet中,如果要获取页面提交的数据要使用requerst.getParameter方法来获取,并且每次需要进行相关的类型转换工作,数据的获取及 ...
- React之key详解
一个例子 有这样的一个场景如下图所示,有一组动态数量的input,可以增加和删除和重新排序,数组元素生成的组件用index作为key的值,例如下图生成的ui展示: 上面例子中的input组件渲染的代码 ...
- Eclipse导入Android签名
本篇主要参照http://blog.csdn.net/wuxy_shenzhen/article/details/20946839 在安装安卓apk时经常会出现类似INSTALL_FAILED_SHA ...
- Python 基础三 文件 函数
今天回顾一下之前学的文件操作相关知识点,对于文件的操作,主要有一下几部分构成: 一.文件的基础知识 1.文件操作的基本流程 文件操作其实可以分成三大部分: 1.打开文件,获取文件句柄并赋予一个变量 2 ...
- npm install fetchmatedata慢的解决办法
最近在开发webpack工程时,第一步npm install这里超级慢,一直停着,显示:"fetchMetadata: sill mapToRegistry uri https://regi ...
- time元素定义的格式
time元素表示的是24小时中的某一个时刻或日期,表示时间时允许时差. time元素定义的格式如下: <time datetime="2016-6-15">2016年6 ...
- iframe框架的应用
同学接私活,我写几个页面. 后台系统,点击侧栏菜单后,右边div的要显示对应的内容.就是说,没选一下左边的菜单,右边的内容都要变化. 这次首先尝试了一下Oldfasional办法--iframe框架. ...
- 欲练JS,必先攻CSS——前端修行之路
今天我讲的主题是css,具体聊一下我大概的css学习历史,分享一些干货,希望这次分享对大家有所启发和帮助. 个人的css历史: 说说自己的css学习的历史,12年,当时是老师手把手1对1教我div+f ...
- mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map
用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...
- Struts2之2.5.10.1HelloWorld
Struts2.5.10.1是目前为止最新的版本,struts2建议持续跟进,理由大家都懂.好了,下面步入正题. 基于struts2.5.10.1建立一个HelloWorld,基于注解的哈! 工具:e ...