zoukankan      html  css  js  c++  java
  • 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 ≤ 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渐渐作出了点套路。。

    题意就是有一种数叫做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;
    }


  • 相关阅读:
    Oracle expdp 多表导出处理
    字符串
    Java设计模式
    多线程
    Java面向对象练习
    Java面向对象基础
    Java基础算法
    Java常识
    DOS基础命令(1)
    Java基础练习
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136059.html
Copyright © 2011-2022 走看看