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

      

  • 相关阅读:
    虎虎的小尾巴:作为交易员,从最初的新人到如今实现稳定盈利,一路走来你有过什么记忆非常深刻的亏损教训和体会? 2018-08-09
    虎虎的小尾巴:期货市场的资金吸引力?
    虎虎的小尾巴
    虎虎的小尾巴:高质量的期货研究报告去哪里找?
    虎虎的小尾巴:如何看待2017.8.7PTA1801一分钟内闪崩下跌6%?
    虎虎的小尾巴:交易逻辑重于一切 2017-08-13
    虎虎的小尾巴:期货数据入门
    虎虎的小尾巴:期货交易在使用基本面分析操作过程中要注意哪些问题?怎么控制风险? 2017-03-11
    Android开发-- 使用ADT23 的一些问题
    Git 学习笔记--3.EGit使用手册
  • 原文地址:https://www.cnblogs.com/Przz/p/5812356.html
Copyright © 2020-2023  润新知