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. oracle PL、SQL(二)

    oracle PL.SQL(基础知识点二) --1,参数 in:表示输入类型,可以省略 :out:输出类型不能省略---------- ----案例1:编写一个过程,可以输入雇员的编号,返回该雇员的姓 ...

  2. 关于发布WP 8.1应用信息不匹配问题的解决办法

    错误提示:   与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...

  3. 移动端1px边框伪类宽高计算

    移动端1px边框在手机上看显得比较粗,于是我们用伪类结合css3缩放的方法去设置线条,但是如果设置div的一条边,水平线就设置宽度100%,垂直线就设置高度100%,那么如果是div的四条边呢?宽高1 ...

  4. RxJava尝试取代Handler初探

    在之前的一篇文章中,我们探究了RxJava的使用方法,详细请看https://www.cnblogs.com/yanyojun/p/9745675.html 根据扔物线大神的描述,如果用一个词来概括R ...

  5. findFile的用法

    ===============================================   @echo off   echo **No Options:   for /f %%a in (&q ...

  6. 【C++】双边滤波器(bilateral filter)

    Bilateral Filtering for Gray and Color Images 双边滤波器:保留边界的平滑滤波器. 在局部上,就是在灰度值差异不大的区域平滑,在灰度值差异比较大的边界地区保 ...

  7. Android Studio -自定义LogCat的颜色

    博文地址 http://www.cnblogs.com/Loonger/p/6285344.html 先看看效果 (设置中的显示,下图) 步骤如下 File->Settings 或Ctrl + ...

  8. CentOS 6.5下安装Python 3.5.2(与Python2并存)

    CentOS 6.5下安装Python 3.5.2(与Python2并存) 安装步骤 1,准备编译环境(环境不对,在安装过程中可能遇到各种问题.比如wget无法下载链接的文件) yum groupin ...

  9. 【转】DLL中导出函数的两种方式(dllexport与.def文件)

    DLL中导出函数的两种方式(dllexport与.def文件) DLL中导出函数的声明有两种方式: 一种方式是:在函数声明中加上__declspec(dllexport):另外一种方式是:采用模块定义 ...

  10. spring boot 自动生成mybatis代码

    1)在pom.xml中增加generator插件 <!--自动生成mybaits--> <plugin> <groupId>org.mybatis.generato ...