• HDOJ 3709 Balanced Number


    数位DP。。。

    Balanced Number

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 1337    Accepted Submission(s): 583


    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
     

    Source
     

    Recommend
    zhengfeng
     
     

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    typedef long long int LL;

    LL x,y,dp[20][20][2000];
    int bit[20];

    LL dfs(int pos,int o,int sum,int limit)
    {
        if(sum<0return 0;
        if(pos==-1return sum==0;
        if(!limit&&~dp[pos][o][sum]) return dp[pos][o][sum];
        int end=limit?bit[pos]:9;
        LL ans=0;
        for(int i=0;i<=end;i++)
        {
            ans+=dfs(pos-1,o,sum+i*(pos-o),limit&&i==end);
        }
        if(!limit)
            dp[pos][o][sum]=ans;
        return ans;
    }

    LL calu(LL x)
    {
        int pos=0;
        while(x)
        {
            bit[pos++]=x%10;
            x/=10;
        }
        LL ans=0;
        for(int o=0;o<pos;o++)
        {
            ans+=dfs(pos-1,o,0,true);
        }
        return ans-pos;
    }

    int main()
    {
        int t;
        scanf("%d",&t);
        memset(dp,-1,sizeof(dp));
        while(t--)
        {
            scanf("%I64d%I64d",&x,&y);
            printf("%I64d ",calu(y)-calu(x-1));
        }
        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Codeblocks )


  • 相关阅读:
    统计代码行数
    梯度下降算法
    multiplot 安装与配置
    ros 源码安装
    cmake 指定gcc/g++版本
    python 科学计算基础库安装
    协方差矩阵的含义
    pysvn 相关
    void 0与undefined
    BEM规范
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350826.html
Copyright © 2020-2023  润新知