【HDU3709】Balanced Number 数位DP

12人阅读 评论(0) 收藏 举报
分类:

Balanced Number

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 7089 Accepted Submission(s): 3404

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

题解

数位DP学习笔记
我们只需要枚举中间点所在的位置,就可以进行DP了;
假如当前的位在中间点的前面我们就加上这个点到中间点的位数;反之减去;
注意在dfs的时候要判断如果当前状态已经是负的了,就要返回,一是状态已经不合法了;二是在dp数组中不能有负下标。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define ll long long 
using namespace std;
int n;
ll dp[20][20][10000];
int a[30];
int pos;
ll dfs(int pos,int balance,int sum,int limit)
{    
    if(pos==-1) 
        return sum==0;
    if(sum<0) 
        return 0;
    if(dp[pos][balance][sum]!=-1&&(!limit)) 
        return dp[pos][balance][sum];
    int up= limit ? a[pos] : 9;
    ll Tmp=0;
    for(int i=0;i<=up;i++)
        Tmp+=dfs(pos-1,balance,sum+(pos-balance)*i,limit && i==up);
    if(!limit) 
        dp[pos][balance][sum]=Tmp;
    return Tmp;
}
ll solve(ll x)
{
    pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    ll sum=0;
    for(int i=pos-1;i>=0;i--)
        sum+=dfs(pos-1,i,0,true);
    return sum-pos;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        ll x,y;
        scanf("%I64d%I64d",&x,&y);
        printf("%I64d\n",solve(y)-solve(x-1));
    }
    return 0;
}
查看评论

hdu3709Balanced Number【数位dp】

Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In...
  • zhou_yujia
  • zhou_yujia
  • 2016-03-11 22:36:01
  • 371

hdu3709 Balanced Number(数位dp)

求区间[L, R]内平衡数的个数  仍然采用 [0, R] - [0, L-1]的形式 平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; ...
  • u013008291
  • u013008291
  • 2015-06-17 21:08:07
  • 1156

HDU3709 Balanced Number 数位DP

学长扔来一份数位DP的题目列表,继续刷。 zheget
  • azx736420641
  • azx736420641
  • 2014-11-20 22:02:03
  • 294

hdu3709 Balanced Number(数位DP)

题目大意:定义一个平衡数为某个数选定其中一位为支点之后,两边的每一位乘上力矩之后的和相等,问区间【a,b】里有多少个平衡数;思路:看样子就是个数位DP。。orz首先,如果能证明出对于每个数来说,如果它...
  • ddjing_
  • ddjing_
  • 2017-08-24 19:29:34
  • 70

hdu3709 Balanced Number 数位dp

题意:定义一个数为“balanced number” 当其满足存在一个数位pos(平衡点),在pos左边的数位的值乘与pos位的距离值的总和等于右 边的数位的值乘与pos位的距离值的总和,给定一个区...
  • u011645923
  • u011645923
  • 2015-01-24 17:33:00
  • 1152

[HDU3709]Balanced Number && 数位DP

递推四维状态写的我无比纠结 最后无奈弃坑 搞记忆化搜索了 这道题多的一个是要枚举支点o 然后从高位往低位考虑的时候注意力矩是否为正值 #include #include #include using...
  • shiyukun1998
  • shiyukun1998
  • 2014-12-22 12:48:30
  • 222

HDU3709:Balanced Number(数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others...
  • junior19
  • junior19
  • 2017-02-06 18:29:34
  • 82

hdu3709——Balanced Number(数位dp)

Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is ...
  • blue_skyrim
  • blue_skyrim
  • 2016-09-23 18:13:02
  • 175

数位dp hdu3709 Balanced Number

题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数 思路: 自己一开始的状态搞错了,dp[i][j]表示第i位为第一个数,j为i到...
  • meixiuxiudd
  • meixiuxiudd
  • 2015-09-09 01:49:25
  • 240

HDU3709 Balanced Number 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意:定义平衡数:以一个数的某一位为支点,其左右各个位分别乘以力矩的和是相等的。比...
  • AC_Gibson
  • AC_Gibson
  • 2015-10-20 21:39:11
  • 210
    个人资料
    持之以恒
    等级:
    访问量: 1934
    积分: 792
    排名: 6万+
    友链