数位DP。。。

Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1337    Accepted Submission(s): 583

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
 

Author
GAO, Yuan
 

Source
 

Recommend
zhengfeng
 
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long int LL;

LL x,y,dp[20][20][2000];
int bit[20];

LL dfs(int pos,int o,int sum,int limit)
{
    if(sum<0) return 0;
    if(pos==-1) return sum==0;
    if(!limit&&~dp[pos][o][sum]) return dp[pos][o][sum];
    int end=limit?bit[pos]:9;
    LL ans=0;
    for(int i=0;i<=end;i++)
    {
        ans+=dfs(pos-1,o,sum+i*(pos-o),limit&&i==end);
    }
    if(!limit)
        dp[pos][o][sum]=ans;
    return ans;
}

LL calu(LL x)
{
    int pos=0;
    while(x)
    {
        bit[pos++]=x%10;
        x/=10;
    }
    LL ans=0;
    for(int o=0;o<pos;o++)
    {
        ans+=dfs(pos-1,o,0,true);
    }
    return ans-pos;
}

int main()
{
    int t;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));
    while(t--)
    {
        scanf("%I64d%I64d",&x,&y);
        printf("%I64d\n",calu(y)-calu(x-1));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 3709 Balanced Number的更多相关文章

  1. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  2. hdu 3709 Balanced Number(平衡数)--数位dp

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  3. HDU - 3709 - Balanced Number(数位DP)

    链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...

  4. hdu 3709 Balanced Number(数位dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:给定区间[a,b],求区间内平衡数的个数.所谓平衡数即有一位做平衡点,左右两边数字的力矩相 ...

  5. HDU 3709 Balanced Number

    发现只要Σa[i]*i%Σa[i]==0就可以. #include<iostream> #include<cstdio> #include<cstring> #in ...

  6. HDU 3709 Balanced Number(数位DP)题解

    思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...

  7. HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)

    平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...

  8. HDU 3709 Balanced Number (数位DP)

    题意: 找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数. 思路: 一开始以为需要枚举位数,枚举前缀和,枚举后缀和,一旦枚举起来就会M ...

  9. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

随机推荐

  1. sublime配置java编译环境

    Windows下配置Sublime Text3的Java环境 字数507 阅读2301 评论2 喜欢2 Sublime Text3是一个比较好用的IDE.截图如下: java环境截图 下面就简单介绍下 ...

  2. LaTeX test

    \begin{equation} x^2+1=2 \end{equation} \begin{equation} x^2+y=3 \end{equation} $\dfrac{2\pi}{N}$

  3. TypeScript Class(类)

    传统的JavaScript注重用函数和基于原型的继承来创建可复用的组件,但这可能让用习惯面对对象方式的程序员感到棘手,因为他们的继承和创建对象都是由类而来的.从JavaScript的下一个版本,ECM ...

  4. Beta版本——项目测试

    前端测试 一.测试用例(tutor_distribution_0001) 测试内容 获取下拉框的输入测试 测试代码 $("#sub-confirm").click(function ...

  5. 关于 客户端发现响应内容类型为“text/html; charset=utf-8”,但应为“text/xml”的解决方法

    http://www.cnblogs.com/jams742003/archive/2008/10/30/1322761.html 请求web服务时,会有如题的异常出现,解决方法如下: 1 检查web ...

  6. mongodb嵌套查询

    db.dbModel.find({'Missions.Rewards.PrizeType':21} )

  7. MySQL外键使用需要注意的几点

    最近刚刚接触MySQL,在建立表示遇到了一些问题,总是提示错误代码:150 can't create table ...,所以就到网上搜索了一下发现还有以下几点需要注意的: [CONSTRAINT [ ...

  8. Java简单的系统登陆

    class Check{ public boolean validate(String name,String password){ if(name.equals("lixinghua&qu ...

  9. Java Connection.setAutoCommit

    Java setAutoCommit 默认为true,即每条SQL语句在各自的一个事务中执行. 很多时候需要有多个操作在一个事务执行,如循环插入,此时可在插入开始前设置 conn.setAutoCom ...

  10. History(历史)命令用法

    如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的用法. 使用 HISTTIMEFORMAT 显示时间 ...