zoukankan      html  css  js  c++  java
  • hdu3709 Balanced Number

    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
    这是一道数位dp,建立一个数组dp[pos][zhidian][liju],pos代表当前位置,zhidian代表取的支点,liju代表到当前位置累加的力矩,这个数组表示这些状态下的总个数,初始化为-1。然后注意有上限,上限的意思是:如果范围是[1,12345],现在找到123,那么仍然有限制,如果再找2变成1232,那么此时没有限制,因为下一位可以找到9.
    <pre name="code" class="cpp">#include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    #define inf 99999999
    #define pi acos(-1.0)
    #define maxn 1000050
    #define MOD 1000000007
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    int len;
    int wei[22];
    ll dp[20][20][2000];
    
    ll dfs(int pos,int o,ll pre,int flag){
        int i,j;
        if(pos==0){
            if(pre==0)return 1;
            return 0;
        }
        if(pre<0)return 0;
        if(flag==0 && dp[pos][o][pre]!=-1){
            return dp[pos][o][pre];
        }
        ll ans=0;
        int ed=flag?wei[pos]:9;
        for(int i=0;i<=ed;i++){
            int next=pre;
            next+=(pos-o)*i;
            ans+=dfs(pos-1,o,next,flag&&i==ed );
        }
        if(!flag)dp[pos][o][pre]=ans;
        return ans;
    }
    
    
    ll solve(ll x)
    {
        if(x==-1)return 0;
        if(x==0)return 1;
        int i,j,o;
        ll t=x;
        len=0;
        while(t){
            wei[++len]=t%10;
            t/=10;
        }
        ll ans=0;
        for(o=len;o>=1;o--){
            ans+=dfs(len,o,0,1);
        }
        ans-=(len-1);
        return ans;
    }
    
    
    int main()
    {
        ll n,m;
        int i,j,T;
        memset(dp,-1,sizeof(dp));
        scanf("%d",&T);
        while(T--)
        {
            scanf("%lld%lld",&n,&m);
            printf("%lld
    ",solve(m)-solve(n-1));
        }
        return 0;
    }
    


    
                
    
  • 相关阅读:
    将包含<pre>标签的json转换成js
    JS 获取系统时间
    JS 对url进行编码
    DevExpress.XtraCharts.ChartControl 实例
    VB 窗体继承
    datagrid 属性
    SQLite操作总结
    Struts2 简介及学习方法介绍
    struts2 中使用DMI(动态调用方法)方式配置action
    JSP 里 的 basePath
  • 原文地址:https://www.cnblogs.com/herumw/p/9464838.html
Copyright © 2011-2022 走看看