HDU 3709 Balanced Number [数位DP]【动态规划】

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3709
——————————.

Balanced Number

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

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

Author
GAO, Yuan

Source
2010 Asia Chengdu Regional Contest
————————–.
题目大意:
就是求[m,n]区间内的平衡数有多少
平衡数的定义为
在一个数中 找一个确定一位数字为”支点” 然后两边的每位数字的值乘上力矩(就是这位到支点的距离)的和相等 这样的数被的定义为平衡数;

解题思路:
还是标准的数位DP
只是要明确的一点是 同一个数 如果为平衡数的话 那么它的支点有且仅有一个 就是说两个数不能被计算两次;
所以在进行DP的时候只要枚举支点即可

还有就是两边的和相等 可以转化为两边的和为0 (只要规定了力矩的方向就能完成)
比如说1234321 支点很明显是4 然后1*3+2*2+3*1+3*(-1)+2*(-2)+1*(-3)==0;

上述明白了就可以DP了 鄙某用的是记忆化搜索的形式写的

dp[位数][力矩总和][支点的位数];

dfs(当前位数,支点,力矩总和,数位的限制);

附本题代码
————————.

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int MOD  = 1000000007;
const int maxn = 200010;

int num[70],len;
LL  dp[20][2000][20];

LL dfs(int pos,int o,int pre,int limit)
{
    if(pos < 0)   return pre==0;
    if(pre<0) return 0;
    if(dp[pos][pre][o]!=-1&&!limit)
        return dp[pos][pre][o];

    int endi = 9;
    if(limit) endi = num[pos];

    LL res = 0;
    for(int i=0; i<=endi; i++)
        res+=dfs(pos-1,o,pre+i*(pos-o),limit&&(i==endi));

    if(!limit) dp[pos][pre][o] = res;
    return res;
}

LL solve(LL n)
{
    len = 0;

    while(n)
    {
        num[len++] = n%10;
        n /= 10;
    }

    LL res = 0;
    for(int i=0;i<len;i++)
        res += dfs(len-1,i,0,1);

    return res-len+1;
}Balanced Number

int main()
{
    memset(dp,-1,sizeof(dp));

    int _,p=0;
    scanf("%d",&_);
    while(_--)
    {
        LL n,m;
        scanf("%lld%lld",&m,&n);
       // printf("%lld %lld\n",solve(n),solve(m-1));
        printf("%lld\n",solve(n)-solve(m-1));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值