链接:

https://vjudge.net/problem/HDU-3709

题意:

A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. Then the torques of left part and right part can be calculated. It is balanced if they are the same. A balanced number must be balanced with the pivot at some of its digits. For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 42 + 11 = 9 and 9*1 = 9, for left part and right part, respectively. It's your job

to calculate the number of balanced numbers in a given range [x, y].

思路:

刚开始没算具体大小。。以为直接算会暴空间。

记录当前的力矩和,让高位表示正数,这样在从高位到低位的时候,如果力矩边成负值,可以直接返回0,因为低位的权值是负的。

同时要枚举每一个位置作为支点的情况,再去减掉每次算的0.

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10; LL F[20][20][1600];
LL dig[20];
LL a, b; LL Dfs(int pos, int piv, int sum, bool lim)
{
if (pos == -1)
return sum == 0;
if (sum < 0)
return 0;
if (!lim && F[pos][piv][sum] != -1)
return F[pos][piv][sum];
int up = lim ? dig[pos] : 9;
LL ans = 0;
for (int i = 0;i <= up;i++)
ans += Dfs(pos-1, piv, (pos-piv)*i+sum, lim && i == up);
if (!lim)
F[pos][piv][sum] = ans;
return ans;
} LL Solve(LL x)
{
int p = 0;
while(x)
{
dig[p++] = x%10;
x /= 10;
}
LL ans = 0;
for (int i = 0;i < p;i++)
ans += Dfs(p-1, i, 0, true);
return ans-(p-1);
} int main()
{
// freopen("test.in", "r", stdin);
memset(F, -1, sizeof(F));
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lld%lld", &a, &b);
printf("%lld\n", Solve(b)-Solve(a-1));
} return 0;
}

HDU - 3709 - Balanced Number(数位DP)的更多相关文章

  1. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  2. hdu 3709 Balanced Number(平衡数)--数位dp

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  3. HDU 3709 Balanced Number(数位DP)题解

    思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...

  4. HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)

    平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...

  5. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  6. HDU 5787 K-wolf Number 数位DP

    K-wolf Number Problem Description   Alice thinks an integer x is a K-wolf number, if every K adjacen ...

  7. hdu3709 Balanced Number (数位dp+bfs)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  8. HDU3709:Balanced Number(数位DP+记忆化DFS)

    Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...

  9. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

随机推荐

  1. Java开发笔记(一百一十六)采用UDP协议的Socket通信

    前面介绍了如何通过Socket接口传输文本与文件,在示例代码中,Socket客户端得先调用connect方法连接服务端,确认双方成功连上后才能继续运行后面的代码,这种确认机制确保客户端与服务端的的确确 ...

  2. 一个长耗时SQL在TiDB和Mysql上的耗时测试

    之前看到的TiDB和MySql的性能对比都是大量短耗时请求下的压测,单机情况下TiDB和MySql的确有些差距,不过笔者最近碰到的场景更多是sql要扫描的行数不小的情况下单sql比较耗时的问题,所以自 ...

  3. Spring Cloud--实现Eureka的高可用(Eureka集群搭建)实例

    将10086注册到10087上: 再在10086服务的基础上复制一个Eureka的服务,端口为10087,将其注册到10086上: application-name的名称保持一致,只是一个服务的两个实 ...

  4. robotFramework_Jquery语法

    向您的页面添加 jQuery 库 jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数.那么我们怎么判断页面中是否使用了Jquery库呢? 如下图,当页面中引入 ...

  5. java之struts2之异常处理

    1.在应用项目中,异常的出现时很正常的.而且项目上线后发生异常也很正常的.那么需要对这些异常有相应的处理机制,以便客户能够看你到更加友好的界面.Struts2中提供了异常处理机制. 2.Struts中 ...

  6. console.log()、console.info()、console.debug()的区别

    onsole.log().console.info().console.debug()的作用都是在浏览器控制台打印信息的. 使用最多的是console.log().console.info()和con ...

  7. 【错误集】类ExcelExport是公共的, 应在名为 ExcelExport.java 的文件中声明

    检查类名是否相同 区分大小写,复制代码的时候会连类名也复制了,哈哈哈,总结一下

  8. 用js写的简单的下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 二十二、mysql索引原理详解

    背景 使用mysql最多的就是查询,我们迫切的希望mysql能查询的更快一些,我们经常用到的查询有: 按照id查询唯一一条记录 按照某些个字段查询对应的记录 查找某个范围的所有记录(between a ...

  10. 关于linux的档案随笔

    刚才在看鸟哥的linux的私房菜,然后看到的部分是关于linux的档案配置部分,之前就强调过,在linux中所有的一切都是以档案的形式存在的,不过不同的文件有一定的区别,linux中是有副档案这个说法 ...