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的更多相关文章

随机推荐

  1. Python open()

    Python中对open()函数的说明: Definition : open(name[, mode[, buffering]]) Type : Function of __builtin__ mod ...

  2. Spring事务管理的实现方式之编程式事务与声明式事务详解

    原创说明:本博文为原创作品,绝非他处转载,转载请联系博主 1.上篇文章讲解了Spring事务的传播级别与隔离级别,以及分布式事务的简单配置,点击回看上篇文章 2.编程式事务:编码方式实现事务管理(代码 ...

  3. AJAX应用案例之省市联动

    jsp 主要是要注意多Document的操作 <%-- Created by IntelliJ IDEA. User: YuWenHui Date: 2017/4/23 0023 Time: 1 ...

  4. Hadoop安全机制之令牌

    介绍 Hadoop中的安全机制包括认证和授权.而Hadoop RPC中采用SASL(Simple Authentication and Security Layer,简单认证和安全层)进行安全认证,具 ...

  5. VueJS 事件修饰符

    事件修饰符 在事件处理程序中调用 event.preventDefault() 或 event.stopPropagation()是非常常见的需求.尽管我们可以在 methods 中轻松实现这点,但更 ...

  6. hdu4417 Super Mario

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  7. VS2017使用 SQL Localdb问题两则

    VS2017使用 SQL  Localdb问题两则 来博客园不算短了,总是索取者.最近使用VS2017,遇到一些问题,在解决时总是找不到拿来就能用的解决方案,在多方寻找,自行探索下,总算圆满解决,在这 ...

  8. 基于ABP框架的权限设置

    需求:在界面展示中,"定向包管理","竞价管理","竞拍管理","发布定向资源","添加竞价资源", ...

  9. Day1 Python 介绍及基础

    ******************本文目录******************一.Pyhon基本介绍 1.Why Python? 2. Python 是一门什么样的语言? 3.Python的优缺点: ...

  10. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...