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));
        }
    }
  • 相关阅读:
    scikit-image 库简介
    如何下载网易云音乐APP里的MV和短视频?
    CentOS上用Squid搭建HTTP代理小结
    超简单的晃咖、小咖秀视频去水印下载方法
    自媒体攻略合集,教你如何做一名能赚钱的自媒体人
    怎么下载映客的视频?映客小视频下载到本地的方法
    如何把手机app的视频下载到手机上?网页上的视频怎么下载?
    短视频如何制作?如何下载短视频?常用的短视频录制和剪辑App有哪些?
    JVM jinfo命令(Java Configuration Info) 用法小结
    iOS11有哪些新功能?旧iPhone是否真的变慢了
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5656642.html
Copyright © 2011-2022 走看看