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));
        }
    }
  • 相关阅读:
    阿里云centos7.2自己安装mysql5.7远程不能访问解决方案
    Delphi中的线程类
    简单说说Delphi中线程的释放
    delphi杀进程的两种方式
    delphi备份恢复剪切板(使用了GlobalLock API函数和CopyMemory)
    Delphi 7下使用Log4Delphi 0.8日志组件
    Demo+在Linux下运行(CentOS7+dotnetcore sdk)
    反射
    解析表达式树
    JS面向对象编程之:封装、继承、多态
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5656642.html
Copyright © 2011-2022 走看看