• HDU 3709 Balanced Number (数位DP)


    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].

    InputThe 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).OutputFor 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

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 typedef long long LL;
     6 int bit[25];
     7 LL dp[20][20][2010], n, m;
     8 
     9 LL dfs(int pos, int d, int sum, int limit) {
    10     if (pos <= 0) return (sum == 0);
    11     if (sum < 0)  return 0;
    12     if ( !limit && dp[pos][d][sum] != -1 ) return dp[pos][d][sum];
    13     LL ans = 0;
    14     int num = limit ? bit[pos] : 9;
    15     for (int i = 0 ; i <= num ; i++)
    16         ans += dfs(pos - 1, d, sum + i * (pos - d), limit && i == num ) ;
    17     if (!limit) dp[pos][d][sum] = ans;
    18     return ans;
    19 }
    20 LL solve(LL x) {
    21     int len = 0;
    22     while(x) {
    23         bit[++len] = x % 10;
    24         x /= 10;
    25     }
    26     LL ret = 0;
    27     for (int i = 1 ; i <= len ; i++)
    28         ret += dfs(len, i, 0, 1);
    29     return ret - len + 1;
    30 }
    31 int main() {
    32     int t;
    33     scanf("%d", &t);
    34     memset(dp, -1, sizeof(dp));
    35     while(t--) {
    36         scanf("%lld%lld", &n, &m);
    37         printf("%lld
    ", solve(m) - solve(n - 1));
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    【题解】SCOI2007组队
    【题解】Atcoder AGC#16 E-Poor Turkeys
    整数拆分 [dp+多项式插值]
    王子 [费用流]
    [ARC083F] Collecting Balls [建二分图+环套树定向+建拓扑图+树的拓扑序计数]
    [BZOJ3196] 二逼平衡树 [权值线段树套位置平衡树]
    [TJOI2017][bzoj4889] 不勤劳的图书管理员 [线段树套线段树]
    [HNOI2015][bzoj4009] 接水果 [整体二分+扫描线]
    [bzoj3065] 带插入区间第k小值 [重量平衡树套线段树]
    [luogu3676] 小清新数据结构题 [树链剖分+线段树]
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9345752.html
Copyright © 2020-2023  润新知