Problem Description
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 4*2 + 1*1 = 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].
 
Input
The input contains multiple test cases. The first line is the total number of cases T (0 < T ≤ 30). For each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 10
18).
 
Output
For each case, print the number of balanced numbers in the range [x, y] in a line.
 
Sample Input
2
0 9
7604 24324
 
Sample Output
10
897
 

题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数

思路:按位枚举,找出所有可能的状况进行dfs,与POJ3252类似

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int bit[19];
__int64 dp[19][19][2005];
//pos为当前位置
//o为支点
//l为力矩
//work为是否有上限
__int64 dfs(int pos,int o,int l,int work)
{
if(pos == -1)
return l == 0;//已经全部组合完了
if(l<0)//力矩和为负,则后面的必然小于0
return 0;
if(!work && dp[pos][o][l]!=-1)//没有上限,且已经被搜索过了
return dp[pos][o][l];
__int64 ans = 0;
int end = work?bit[pos]:9;//有上限就设为上限,否则就设为9
for(int i=0; i<=end; i++)
{
int next = l;
next += (pos-o)*i;//力矩
ans+=dfs(pos-1,o,next,work&&i==end);
}
if(!work)
dp[pos][o][l] = ans;
return ans;
} __int64 solve(__int64 n)
{
int len = 0;
while(n)
{
bit[len++] = n%10;
n/=10;
}
__int64 ans = 0;
for(int i = 0; i<len; i++)
{
ans+=dfs(len-1,i,0,1);
}
return ans-(len-1);//排除掉0,00,000....这些情况
} int main()
{
int T;
__int64 l,r;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--)
{
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",solve(r)-solve(l-1));
} return 0;
}

HDU3709:Balanced Number(数位DP+记忆化DFS)的更多相关文章

  1. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  2. HDU3709 Balanced Number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others)     ...

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

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

  4. hdu3709 Balanced Number 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...

  5. 数位dp/记忆化搜索

    一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an  ...

  6. HDU 3709 Balanced Number (数位DP)

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

  7. [hihocoder 1033]交错和 数位dp/记忆化搜索

    #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1 ...

  8. 【poj1850】 Code 数位dp+记忆化搜索

    题目大意:给你一个字符串,问你这个字符串的rank,如果这个字符串不合法,请直接输出0.(一个合法的字符串是对于∀i,有c[i]<c[i+1]) 字符串s的rank的计算方式:以字符串长度作为第 ...

  9. [BZOJ3598][SCOI2014]方伯伯的商场之旅(数位DP,记忆化搜索)

    3598: [Scoi2014]方伯伯的商场之旅 Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 449  Solved: 254[Submit][Sta ...

随机推荐

  1. WinForm界面中快捷键设置

    这是对整个界面的快捷键的设置,比如查询,保存. 1 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if ...

  2. [转]组合数取模 Lucas定理

    对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...

  3. ZOJ 2968 Difference Game 【贪心 + 二分】

    题意: 有Ga.Gb两堆数字,初始时两堆数量相同.从一一堆中移一一个数字到另一一堆的花费定义为两堆之间数 量差的绝对值,初始时共有钱C.求移动后Ga的最小小值减Gb的最大大值可能的最大大值. 思路: ...

  4. 界面控件 - 滚动条ScrollBar(对滚动条消息和鼠标消息结合讲的不错)

    界面是人机交互的门户,对产品至关重要.在界面开发中只有想不到没有做不到的,有好的想法,当然要尝试着做出来.对滚动条的扩展,现在有很多类是的例子. VS2015的代码编辑是非常强大的,其中有一个功能可以 ...

  5. oschina娱乐游戏

    休闲游戏 123网络游戏 88单机游戏 201游戏模拟器/工具/引擎

  6. Java网络蜘蛛/网络爬虫 Spiderman

    Spiderman - 又一个Java网络蜘蛛/爬虫 Spiderman 是一个基于微内核+插件式架构的网络蜘蛛,它的目标是通过简单的方法就能将复杂的目标网页信息抓取并解析为自己所需要的业务数据. 主 ...

  7. JS - 按钮倒计时

    效果: html代码: <input type="button" id="btn" value="点击获取效验码" /> js代 ...

  8. TOMCAT的域名配置

    链接地址:http://blog.163.com/skk1987@126/blog/static/29303413201051383548377/ 现在很多的公司的网站都是用tomcat作为应用服务区 ...

  9. 第三方系统打开EAFC的实现

    前言:EAFC是我们公司的一个框架,一个项目上,客户的OA系统要调用我们推送过去的代办任务,希望能打开我们的代办处理界面,我们的代办处理界面是winform的.引出给出了以下的一个方案.在此备存. - ...

  10. SuperSocket源码解析之开篇

    一 简介 官方介绍:SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 Socket 连接和 S ...