zoukankan      html  css  js  c++  java
  • HDU 3709 Balanced Number ZOJ 3416 Balanced Number(数位DP)

    Balanced Number

    Time Limit: 5 Seconds      Memory Limit: 65536 KB

    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 [xy].

    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 [xy] in a line.

    Sample Input

    2
    0 9
    7604 24324
    

    Sample Output

    10
    897
    

    Author: GAO, Yuan
    Source: The 2010 ACM-ICPC Asia Chengdu Regional Contest

    很好的数位DP题目。

    要求是平衡数,增加一维表示力矩。

    /*
     * HDU 3709
     * 平衡数,枚举支点
     * dp[i][j][k] i表示处理到的数位,j是支点,k是力矩和
     */
    
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    long long dp[20][20][2000];
    int bit[20];
    long long dfs(int pos,int center,int pre,bool flag)
    {
        if(pos==-1)return pre==0;
        if(pre<0)return 0;//当前力矩为负,剪枝
        if(!flag&&dp[pos][center][pre]!=-1)
            return dp[pos][center][pre];
        int end=flag?bit[pos]:9;
        long long ans=0;
        for(int i=0;i<=end;i++)
            ans+=dfs(pos-1,center,pre+i*(pos-center),flag&&i==end);
        if(!flag)dp[pos][center][pre]=ans;
        return ans;
    }
    long long calc(long long n)
    {
        int len=0;
        while(n)
        {
            bit[len++]=n%10;
            n/=10;
        }
        long long ans=0;
        for(int i=0;i<len;i++)
            ans+=dfs(len-1,i,0,1);
        return ans-(len-1);//去掉全0的情况
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int T;
        long long x,y;
        memset(dp,-1,sizeof(dp));//这个初始化一定别忘记
        scanf("%d",&T);
        while(T--)
        {
            scanf("%I64d%I64d",&x,&y);
            printf("%I64d\n",calc(y)-calc(x-1));
        }
        return 0;
    }
    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    TCP/IP协议栈概述及各层包头分析
    Maven:Non-resolvable parent POM: Failure to find错误
    mysql alter 用法,修改表,字段等信息
    PowerDesigner16 设置导出sql文件的编码
    linux iptables开放/关闭端口命令
    Enterprise Architect 13 : 需求建模 自动命名并计数
    Enterprise Architect 13 : 将绘制的图形导出成图片 或者 拷贝到剪贴板中
    Enterprise Architect 13 : 设置默认代码环境
    使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法
    PowerDesigner16 修改表或表的字段Name的时候不让Code不自动跟着变
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3052842.html
Copyright © 2011-2022 走看看