Balanced Number

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 ≤ 1018).

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

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
int digit[];
__int64 dp[][][];
__int64 dfs(int pos,int c,int l,int lim)
{
if(pos==) return l==;
if(l<) return ;
if(!lim && dp[pos][c][l]!=-) return dp[pos][c][l];
int n=lim?digit[pos]:;
__int64 ans=;
for(int i=; i<=n; i++)
{
int next=l;
next+=(pos-c)*i;
ans+=dfs(pos-,c,next,lim&&i==n);
}
if(!lim) dp[pos][c][l]=ans;
return ans;
}
__int64 solve(__int64 n)
{
int len=;
while(n)
{
digit[++len]=n%;
n/=;
}
__int64 sum=;
for(int i=; i<=len; i++) sum+=dfs(len,i,,);
return sum-len+;
}
int main()
{
int t;
__int64 x,y;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--)
{
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",solve(y)-solve(x-));
}
return ;
}

另一个带注释的AC代码:

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

hdu3709 Balanced Number (数位dp+bfs)的更多相关文章

  1. HDU3709 Balanced Number —— 数位DP

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

  2. hdu3709 Balanced Number 数位DP

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

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

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

  4. HDU 3709 Balanced Number (数位DP)

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

  5. hdu3709 Balanced Number 树形dp

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  6. Balanced Number 数位dp

    题意: 给出求ab之间有多少个平衡数   4139为平衡数   以3为轴   1*1+4*2==9*1 思路很好想但是一直wa  : 注意要减去前导零的情况 0 00 000 0000   不能反复计 ...

  7. [HDU3709]Balanced Number

    [HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...

  8. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  9. HDU3709 Balanced Number (数位dp)

     Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

随机推荐

  1. nyoj_95_众数问题_map练习

    众数问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重 ...

  2. Python--常见问题解决方案

    1.如何支持中文,在第一行加上编码格式的支持: # coding=gbk +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  3. python基础——单元测试

    python基础——单元测试 如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生. 单元测试是用来对一个模块.一个函数或者一个类来进行正确性检验的 ...

  4. Sample Apps by Android Team -- Amazed

    Sample Apps by Android Team 代码下载:http://pan.baidu.com/s/1eSNmdUE , 代码原地址:https://code.google.com/arc ...

  5. MVC公开课 – 1.基础 (2013-3-15广州传智MVC公开课)

      1.MVC设计模式 Model 是指 要处理的业务代码和数据操作代码 View 视图 主要是指的 跟用户打交道 并能够展示数据 Controller 看成是 Model和View的桥梁 优点: 1 ...

  6. android 入门-android Studio 快捷输入

    1.输入 log的时候按一下Tab.就会打出 private static final String TAG="Settings"; 2. shift +alt+x 运行 shif ...

  7. ASP.NET 5探险(7):使用混合型控制器方便实现单页应用

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于在ASP.NET 5中,MVC和WEB API的技术栈合并了,所以开发混合型Con ...

  8. WPF之MVVM(Step2)——自己实现DelegateCommand:ICommand

    在自己实现MVVM时,上一篇的实现方式基本是不用,因其对于命令的处理不够方便,没写一个命令都需要另加一个Command的类.此篇主要介绍DelegateCommand来解决上面所遇到的问题. 首先,我 ...

  9. 在Salesforce中以PDF的格式显示对应的页面

    在Salesforce中可以简单设置page的属性让页面以pdf的方式显示内容, 当然了我们的page内容可以用Html的方式编写 设置方式为:renderAs="pdf" 请看如 ...

  10. javascript 的基础笔记

    新手入門: alert的使用:   在alert中\xB0可以输出温度(centigrade)的符号,\xNN可以输入一些不能输入的特殊字符,NN是两个十六进制数,表示字符在latin-1 字符集中的 ...