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

InputThe 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 ≤ 10 18).OutputFor 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 题意:一个数,定一个支点,力矩相等为平衡数。
题解:数位dp,f[i][j][k]表示i为,j位为支点,k为当前力矩。
 #include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std; ll f[][][],l,r;
int a[],n,cnt; ll dfs(int wei,int zd,int lj,bool flag)
{
if (wei==) return lj==;
if (lj<) return ;
if (!flag&&f[wei][zd][lj]!=-) return f[wei][zd][lj];
ll res=;
int ed;
if (flag) ed=a[wei];
else ed=;
for (int i=;i<=ed;i++)
res+=dfs(wei-,zd,lj+(wei-zd)*i,flag&&i==ed);
if (!flag) f[wei][zd][lj]=res;
return res;
}
ll solve(ll x)
{
cnt=;
while(x)
{
a[++cnt]=x%;
x/=;
}
ll ans=;
for (int i=;i<=cnt;i++)
ans+=dfs(cnt,i,,);
return ans-(cnt-);
}
int main()
{
int cas;scanf("%d",&cas);
memset(f,-,sizeof(f));
while(cas--)
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-));
}
}

hdu3709 Balanced Number 树形dp的更多相关文章

  1. HDU3709 Balanced Number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others)     ...

  2. hdu3709 Balanced Number (数位dp+bfs)

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

  3. hdu3709 Balanced Number 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...

  4. [HDU3709]Balanced Number

    [HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...

  5. HDU3709 Balanced Number (数位dp)

     Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

  6. HDU3709:Balanced Number(数位DP+记忆化DFS)

    Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...

  7. [暑假集训--数位dp]hdu3709 Balanced Number

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  8. HDU 3709 Balanced Number (数位DP)

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

  9. HDU3709 Balanced Number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...

随机推荐

  1. JVM初探

    ### JVM分为类的加载生命周期和gc垃圾回收两个大的方面#####首先是类的生命周期, 类的加载: --> 记载字节码 ---> 这个过程有类的加载起参与,双亲委托机制() --> ...

  2. Java GUI 基础组件

    1.JLabel  标签 构造函数: JLabel() JLabel(String text) JLabel(String text,int align)     //第二个参数设置文本的对齐方式,常 ...

  3. APM系列-国外新兴厂商New Relic vs. AppDynamics

    前: New Relic的上市使得IT和资本界开始重新重视APM,当然跟传统APM相比,New Relic还是有相当的创新,另外还有一点是目前的创业潮导致的企业级需求增大. In recent yea ...

  4. http以及http协议简单理解

    HTTP协议是超文本传输协议的缩写,是用于从万维网(WWW)服务器传输超文本到本地浏览器的传送协议:HTTP是一个基于TCP/IP通信协议来传递数据(HTML文件, 图片文件, 查询结果等)HTTP作 ...

  5. H5拖拽事件的完整过程和语法

    <!DOCTYPE HTML> <html> <head> <style type="text/css"> #div1 { widt ...

  6. SAP CRM点了附件的超链接后报错的处理方式

    SAP CRM系统里,点击了附件的这些超链接后,如果是文本文件,会在浏览器里打开.如果是其他类型的文件,会弹出下载对话框. 然而最近我工作时遇到一个问题,点击超链接后,总是弹出Logon failed ...

  7. PHP CLI应用的调试原理

    我们在Eclipse里选中一个PHP文件,右键选择Debug As->PHP CLI Application. 所谓CLI应用,是指这种脚本文件不需要任何Web服务器即可运行,当然, PHP运行 ...

  8. 如何修改开发板主频--迅为iMX6UL开发板

    平台:iMX6UL开发板           iMX6UL开发板 可以在文件系统中通过命令修改 CPU 运行的主频.如下图所示,使用命令“cat /sys/devices/system/cpu/cpu ...

  9. OpenJudge_2757:最长上升子序列

    描述一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1 ...

  10. JS 数组间的操作

    JS 数组间的操作(交集,并集.差集) 以下是js数组之间常用的操作,如交集,并集.差集等. 迭代 each是一个集合迭代函数,可以将一个函数作为参数和一组可以选的参数.依次将集合的每一个元素和可选参 ...