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;
    }
    


    
                
    
  • 相关阅读:
    Vivado2014.3安装破解流程
    Win7、Win8、win10系统USB-Blaster驱动程序无法安装的解决办法
    Quartus II 与ModelSim-Altera联合仿真FFT IP核之FFT IP调用与自产生信号分析-lab2
    Quartus II 与ModelSim-Altera联合仿真FFT IP核之FFT IP调用与例程数据验证-lab1
    Quartus II 与ModelSim-Altera联合仿真FFT IP核之FFT IP核分析
    Quartus II 与ModelSim-SE联合仿真Shift_ram
    Quartus II 与ModelSim-Altera联合仿真PLL
    1st.初识GCC——关于GCC编译器的相关语法与介绍
    5th.NandFlash初接触——反正他说这几节课都是启发一下而已
    4th.关于MMU中的虚拟映射
  • 原文地址:https://www.cnblogs.com/herumw/p/9464838.html
Copyright © 2011-2022 走看看