zoukankan      html  css  js  c++  java
  • 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 )


  • 相关阅读:
    寒假了
    【MFC】浏览器中快速打开常用工具
    【转】MFC隐藏进程自身(任务管理器不可见,wSysCheck等工具可见)
    【原】DIY属于自己的鼠标侧键
    coco2dx 3.4final 使用scale9sprite
    linux挂载新硬盘
    关于c语言中的结构体使用偏移量求值问题
    Linux的网卡由eth0变成了eth1,如何修复
    oracle归档管理
    exsi上虚拟因硬盘不足无法启动
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350826.html
Copyright © 2011-2022 走看看