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): 4624    Accepted Submission(s): 2171


    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
    /*
    hdu 3709 Balanced Number(数位dp)
    
    problem:
    求[a,b]平衡数的个数.(以这个数的某一位为支点,另外两边的数字大小乘以力矩).
    
    solve:
    可以枚举支点位置,然后数位dp计算力矩和为0的个数
    
    hhh-2016-08-24 19:54:09
    */
    #pragma comment(linker,"/STACK:124000000,124000000")
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <math.h>
    #include <queue>
    #include <map>
    #define lson  i<<1
    #define rson  i<<1|1
    #define ll long long
    #define clr(a,b) memset(a,b,sizeof(a))
    #define scanfi(a) scanf("%d",&a)
    #define scanfl(a) scanf("%I64d",&a)
    #define key_val ch[ch[root][1]][0]
    #define inf 0x3f3f3f3f
    #define mod 1000003
    using namespace std;
    const int maxn = 100010;
    ll dp[20][20][2005];
    int t[20];
    ll dfs(int len,int o,int all,int flag)
    {
        if(len < 0)
            return all == 0;
        if(all < 0)
            return 0;
        if(dp[len][o][all] != -1 && !flag)
            return dp[len][o][all];
        int n = flag ? t[len]:9;
        ll ans = 0;
        for(int i = 0;i <= n;i++)
        {
            int ta = all;
            ta += (len - o) * i;
            ans += dfs(len-1,o,ta, flag && i == n);
        }
        if(!flag)
            dp[len][o][all] = ans;
        return ans;
    }
    
    
    ll cal(ll b)
    {
    
        int tot = 0;
        while(b)
        {
            int p = b % 10;
            t[tot++] = p;
            b /= 10;
        }
        ll ans = 0;
        for(int i = 0; i <tot; i++)
            ans += dfs(tot-1,i,0,1);
    //    cout <<ans <<endl;
        return ans-(ll)(tot-1);
    }
    
    int main()
    {
        int T;
        ll a,b;
    //    freopen("in.txt","r",stdin);
        scanfi(T);
        memset(dp,-1,sizeof(dp));
        while(T--)
        {
            scanfl(a),scanfl(b);
    //        cout << a <<" " <<b <<endl;
            printf("%I64d
    ",cal(b) - cal(a-1));
        }
    }
    

      

  • 相关阅读:
    常用验证函数isset()/empty()/is_numeric()函数
    jquery select取option的value值发生变化事件
    (转)浅谈HTML5与css3画饼图!
    文本框输入值文字消失常用的两种方法
    简洁的滚动代码(上下滚动)
    (转)PHP的语言结构和函数的区别
    兼容ie7的导航下拉菜单
    jquery中each()函数
    tomcat源码导入eclipse
    weblogic linux环境下新建domain
  • 原文地址:https://www.cnblogs.com/Przz/p/5812356.html
Copyright © 2011-2022 走看看