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 )


  • 相关阅读:
    Fiddler 的几个用法
    jQuery中ajax的使用和缓存问题解决 $getjson 与$get都会被IE缓存
    http与https的区别以及https的加密原理
    asp.net mvc通过预处理实现数据过滤和数据篡改。
    C# winform的WebBrowser非常规编程(强烈推荐)
    DES加密 java与.net可以相互加密解密的方法
    日常开发中常见的HTTP协议的状态码
    前端性能监控
    在手机的浏览器上通过连接打开App
    工作五年总结一二三流公司的常见特性
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350826.html
Copyright © 2011-2022 走看看