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. js中 map 遍历数组

    map 方法会迭代数组中的每一个元素,并根据回调函数来处理每一个元素,最后返回一个新数组.注意,这个方法不会改变原始数组. 在我们的例子中,回调函数只有一个参数,即数组中元素的值 (val 参数) , ...

  2. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  3. 【xml】转义字符 &lt;等符号出现的原因

    来源:http://www.cnblogs.com/hyd309/p/3549076.html HTML中的转义字符  HTML中<, >,&等有特别含义,(前两个字符用于链接签, ...

  4. MyBatis之CRUD

    1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...

  5. 3ds max画曲线 设置摄像机的起始位置

    参考 http://www.3dmax8.com/3dmax/2013/0916/5661.html 如果想创建曲线段,可以在单击下一个点时按住鼠标不放,继续拖曳,再拖到另一个点上,单击鼠标右键,即可 ...

  6. Swift - 键盘弹出样式

    Swift提供了11种键盘类型: 在开发中,我们可以根据不同的需求,选择不同的键盘样式,例如,当我们只需要输入手机号码时,可以选择纯数字类型的键盘(.NumbersAndPunctuation),当我 ...

  7. logstash json和rubydebug 第次重启logstash都会把所有的日志读完 而不是只读入新输入的内容

    查看一下agent端的shipper的配置: # cat logstash_test2.shipper.conf input { file { path => ["/apps/logs ...

  8. .net学习笔记---lambda表达式(自执行方法)

    http://www.cnblogs.com/jesse2013/p/happylambda.html#b034 lambda表达式 http://www.cnblogs.com/OceanEyes/ ...

  9. Delphi中的异常处理

    转载:http://www.cnblogs.com/doit8791/archive/2012/05/08/2489471.html 以前写Delphi程序一直不注意异常处理,对其异常处理的机制总是一 ...

  10. 基于MyEclipse6.5的ssh整合

    1.编写目的 为了学习,为了更好的学习java. 为了让想要学习这个整合的人少走弯路! ! ! 2.实验环境 l MyEclipse6.5 l JBoss4.2.1 l SQL2005 l 数据库脚本 ...