HDU 3709 Balanced Number

Balanced Number

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


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

做数位DP渐渐作出了点套路。。

题意就是有一种数叫做balaced number,他满足吧数的一个位作为支点,左右两边的权值的合相等。

我的想法就是通过记忆化搜索,创建一个数组dp[i][j][k],其中i表示的是位置pos,j表示的是平衡点,k表示的是对于该位置选取这个平衡点时候的权值和(权值和最大估算了一下,最多不超过20 * 20 / 2 * 9)。因为任何一个数都不可能有两个支点,所以我们只需要对这个数字所有的数位假设为端点进行搜索就可以。

搜索按位依次向高位递归,对当前的权值有value = (当前位 - 平衡位) * (当前正在搜索的数字),显而易见,当value小于0的时候就已经不可能是平衡的了。

提交错了好几次,最后发现问题是calc函数中的ans用的是int,有点粗心了- -

代码如下:

/*************************************************************************
	> File Name: Balanced_Number.cpp
	> Author: Zhanghaoran
	> Mail: chilumanxi@xiyoulinux.org
	> Created Time: Fri 13 Nov 2015 10:11:08 PM CST
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

int T;
long long  dp[25][25][3000];
int num[25];


long long work(int pos, int balance, int  value, int flag){
    if(pos == 0){
        if(value == 0)
            return 1;
        else 
            return 0;
    }

    if(value < 0)
        return 0;
    if(!flag && dp[pos][balance][value] != -1){
        return dp[pos][balance][value];
    }
    long long ans = 0;
    int temp = flag ? num[pos] : 9;
    for(int i = 0; i <= temp; i ++){
        ans += work(pos - 1, balance, value + (pos - balance) * i, flag && i == temp);
    }

    if(!flag && dp[pos][balance][value] == -1)
        dp[pos][balance][value] = ans;

    return ans;
}

long long calc(long long x){
    int i = 1;
    long long ans = 0;
    while(x){
        num[i ++] = x % 10;
        x /= 10;
    }
    for(int j = i - 1; j >= 1; j --){
        ans += work(i - 1, j, 0, 1);
    }
    return ans - i;
}

int main(void){
    memset(dp, -1, sizeof(dp));
    scanf("%d", &T);
    long long a, b;
    while(T --){
        cin >> a >> b;
	cout << calc(b) - calc(a - 1) << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值