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): 3988    Accepted Submission(s): 1869


    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
     
    题意是求范围内的平衡数。这个数是像物理中的力矩一样保持平衡的,数字有一个枢轴点,该点两边的数字乘以距离枢轴点的距离的乘积之和相等就称这个数为平衡数。比如24380:3点左边和为2 * 2 + 4 * 1 = 8,右边为8 * 1 + 0 * 2 = 8。所以这个数平衡。
     
    数位dp,枚举枢轴点。
     
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define ll long long
    
    int dig[20];
    ll dp[20][20][2000];
    
    ll dfs(int pos, int pivot, int sum, int lim) {
        if(pos == -1) return sum == 0;
        if(sum < 0) return 0;
        if(!lim && dp[pos][pivot][sum] != -1) return dp[pos][pivot][sum];
        int End = lim ? dig[pos] : 9;
        ll ret = 0;
        for(int i = 0; i <= End; i++) {
            ret += dfs(pos - 1, pivot, sum + i * (pos - pivot), (i == End) && lim);
        }
        if(!lim) dp[pos][pivot][sum] = ret;
        return ret;
    }
    
    ll func(ll num) {
        int n = 0;
        while(num) {
            dig[n++] = num % 10;
            num /= 10;
        }
        ll ret = 0;
        for(int i = 0; i < n; i++) {
            ret += dfs(n - 1, i, 0, 1);
        }
        return ret - n;
    }
    
    int main() {
        int t;
        ll n, m;
        memset(dp, -1, sizeof(dp));
        scanf("%d", &t);
        while(t--) {
            scanf("%I64d %I64d", &n, &m);
            printf("%I64d
    ", func(m) - func(n - 1));
        }
    }
  • 相关阅读:
    如何解决UITextField挡住键盘的问题
    设置UITextField中能输入的最大的字数
    如何判断IOS的设备版本型号
    IOS中如何实现对话聊天
    精美的iOS图片欣赏
    关于IOS 应用图标的设置
    ios7中添加多个按钮
    elementary0.4:快速配置工具
    elementary:网易云音乐白条解决
    deepin下eclipse快捷方式
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5656642.html
Copyright © 2011-2022 走看看