zoukankan      html  css  js  c++  java
  • hdu 3709 Balanced Number (数位dp)

    Balanced Number

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


    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
     

    题意:
    找出一个区间内的平衡数有多少个。
    假设有1<=i<=n,a=a[n]a[n-1]……a[2]a[1],使得t=i-1,s=n-1-t;
    sa[n]+(s-1)a[n-1]+……+2a[n-s+2]+a[n-s+1]=a[t]+2a[t-1]+……+ta[1]则a叫平衡数。

    思路:
    数位dp,有一个巧妙的处理:知道支点后,固定一个左端点,在支点左边的数力臂为正,右边的为负。那么算到最后总力矩为0则这个数为平衡数。(将两边统一了)
    dp[pos][zd][sum]表示当前位置为pos,支点位置为zd,前缀力矩和为sum时的平衡数的个数,递推用记忆化搜索实现。详细见代码。
    注意当力矩为负时,就要返回。后面仅仅可能越变越小了。
    注意不能用四维dp,加一位对上限有无要求。有要求的时候与每一位的值有关了。状态不是唯一的。

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define maxn 1005
    #define MAXN 100005
    #define mod 100000000
    #define INF 0x3f3f3f3f
    #define pi acos(-1.0)
    #define eps 1e-6
    typedef long long ll;
    using namespace std;
    
    ll n,m,ans,tot,len;
    ll dig[20],dp[20][20][2000];  // 当前位置 支点位置 前缀力矩和
    
    ll dfs(ll pos,ll zd,ll sum,ll limit) // 当前位置  支点位置 前缀力矩和 对上届有无限制
    {
        if(sum<0) return 0;
        if(pos==0)
        {
            if(sum==0) return 1;
            return 0;
        }
        if(!limit&&dp[pos][zd][sum]!=-1) return dp[pos][zd][sum];  // 注意这里要对上届无限制
        ll i,j,t,ed,best=0;
        if(limit) ed=dig[pos];
        else ed=9;
        for(i=0;i<=ed;i++)
        {
            best+=dfs(pos-1,zd,sum+(pos-zd)*i,limit&&(i==ed)); // 由下一位的状态递推过来
        }
        if(!limit) dp[pos][zd][sum]=best;    // 记忆化 注意这里要对上届无限制
        return best;
    }
    ll solve(ll u)
    {
        if(u==-1) return 0;
        if(u==0) return 1;
        ll i,j,t,res=0;
        len=0;
        memset(dig,0,sizeof(dig));
        while(u)
        {
            dig[++len]=u%10;
            u/=10;
        }
        for(i=1;i<=len;i++)  // 枚举支点所在的位置
        {
            res+=dfs(len,i,0,1);
        }
        return res-len+1;  // 0算了len遍
    }
    int main()
    {
        ll i,j,t;
        memset(dp,-1,sizeof(dp));
        scanf("%I64d",&t);
        while(t--)
        {
            ll u,v;
            scanf("%I64d%I64d",&u,&v);
            ans=solve(v)-solve(u-1);
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    





  • 相关阅读:
    Using Project Custom Properties to replace all end point of test cases
    Jacoco Code coverage with Robotframework Maven pom.xml example
    Jacoco Code coverage with Robotframework
    Test coverage in a multi module Maven project
    如何让linux的history命令显示时间记录
    项目角色职责分配表
    Linux检查哪些thread在占用CPU
    soapui_mockservicerunner_script.sh
    Some api interface link of Soapui
    GlusterFS分布式存储系统
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7016166.html
Copyright © 2011-2022 走看看