数位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
2010 Asia Chengdu Regional Contest
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 )

转载于:https://www.cnblogs.com/CKboss/p/3350826.html

HDOJ 3709 Balanced Number相关推荐

  1. HDU 3709 Balanced Number

    Balanced Number 题意: 平衡数:存在该数中以一个数字为支点(pivot),点的"力矩"为该点到支点的距离乘以该点的值,而平衡指的是支点两侧的力矩和相等 思路: 易知 ...

  2. HDU 3709 Balanced Number (数位DP)

    题意 求出[x, y] 范围内的平衡数,平衡数定义为:以数中某个位为轴心,两边的数的偏移量为矩,数位权重,使得整个数平衡. 思路 外层枚举平衡点,然后数位DP即可.设计状态: dp[pos][o][l ...

  3. HDU - 3709 (Balanced Number)

    题意:设一个数的十进制表达是 a1a2a3...an ,则它是 Balanced Number 的定义是:存在一个位置 i <=n,使得:  问区间[l,r] 内有多少个数是 Balanced ...

  4. HDU - 3709 Balanced Number(数位dp)

    题目链接:点击查看 题目大意:将一串数字视为天平,两端平衡的数字称为平衡数,并求出一段闭区间中平衡数的个数.所谓的平衡条件即为力臂与 力相乘后两端的数量和可以抵消,例如数字4139可以视为以3为中轴的 ...

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

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

  6. 杭电oj HDOJ 1018 Big Number(斯特林公式求大数阶乘的位数)

    杭电oj HDOJ 1018 Big Number 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Problem Description In ...

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

    题目链接:Problem - 3709 题意:给定区间[a,b],求区间内平衡数的个数.所谓平衡数即有一位做平衡点,左右两边数字的力矩相等. 分析,这道题我们直接对平衡点进行枚举即可,然后分别求出来每 ...

  8. 【HDOJ】1005 Number Sequence_天涯浪子_新浪博客

    [题目]http://acm.hdu.edu.cn/showproblem.php?pid=1005 [报告] 这道明显是数学题,但如果按位枚举显然是要TLE的. 仔细思考,它有一个MOD 7,这个很 ...

  9. [HDOJ]1018. Big Number

    恩,简单的数学题目. 简单推导如下: log10(n!) = log10(n*n-1*n-2......*2*1) = log10(n) + log10(n-1) + log10(n-2) + ... ...

最新文章

  1. centos在线安装svn
  2. 关于业务系统的架构思考
  3. 刷新存储器的容量单位是什么_GD25Q80CSIG|相变存储器是什么,具备什么特点?
  4. 关于项目中的日期提交
  5. Python解析XML文件
  6. 什么是闭合GOP和开放GOP?
  7. 渡虎谷告诉你CSS的结构和规则
  8. CPython 和IronPython的基准测试
  9. 【操作系统】Mac环境配置
  10. Newtonsoft.Json介绍
  11. 第1章 初始JAVA
  12. STM32+ADS1110
  13. 订单可视化(智能制造、流程再造、企业信息化) 第二篇 背景及问题提出
  14. 新服务器网卡识别及地址设置
  15. 平行四边形的效果实现
  16. Git上修改分支名称
  17. 人家出轨你为什么那么嗨
  18. 十进制100转换成八进制是多少?
  19. 2018省赛第九届蓝桥杯真题C语言B第四题题解 测试次数
  20. RT-thread内核之IO设备管理系统

热门文章

  1. LYNC显示用户位置的相关配置
  2. 基于pytorch开发CNN提取全连接层作为特征
  3. 算法导论之NP完全性和近似算法
  4. 一个域名解析到另一个域名_如何申请一个免费的域名?
  5. 字节输入流 InputStream
  6. Python 基础篇-python3安装pyHook和pywin32库
  7. 关于modbus温湿度传感器,IIC热成像仪相关知识点总结
  8. MarkDown常用技巧总结
  9. opencv 图像的腐蚀与膨胀
  10. Cisco Packet Tracer v7.0安装包下载(358MB)