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. Split方法,拆分字符串后,去除返回的空值

         我们在使用Split('')方法的时候,根据指定的 '字符' 对字符串进行拆分,当'字符’为最后一个,将会拆分一个空值进行返回. 这个时候我们可以使用 string.Split(new ch ...

  2. 定时器、线程queue、进程池和线程池

    1.定时器 指定n秒后,执行任务 from threading import Timer,current_thread import os def hello(): print("%s he ...

  3. Qt获取本机IP地址

    Qt获取本机IP地址: Qt版本:4.8.6 #include <QtNetwork/QNetworkInterface.h> QString ipAddr; QList<QHost ...

  4. 几个windows使用小技巧

    windows使用技巧 保存网页上图片时,可以按住左键把图片拖到右下角(win+D,双屏幕直接拖动)然后就可以放在桌面啦 放大镜-->Win+加号或者减号(放大或缩小).Win + Esc(退出 ...

  5. docker部署xxl-job

    资源 xxl-job:1.9.1 docker:17.05.0-ce maven:3.5.0-jdk-8 tomcat:8.5.23.0 mysql:5.6.40 一.创建数据库 克隆项目到服务器下 ...

  6. 前端学习日记-vue cli3.0环境搭建

    卸载老版本的 vue-cli : npm uninstall vue-cli -g 安装新版本的 : npm install -g @vue/cli --安装新版本cli 同时nodeJS 要更新至 ...

  7. poj 1664放苹果(转载,不详细,勿点)(递归)

    题目和别人的解析传送门 我的代码 #include<bits/stdc++.h> using namespace std; int f(int m,int n) { ) ; ||m==) ...

  8. poj1681 Painter's Problem

    题目描述: 和那道关灯差不多,求最少涂几次. 题解: 高消,然后深搜枚举自由元更新答案. 貌似这道题没卡贪心但是其他题基本都卡了. 比如$Usaco09Nov$的$lights$ 代码: #inclu ...

  9. 关于U盘安装ubuntu-18.04安装时候出现的grub-efi-amd64-signed的问题。

    关于这个问题,首先我们要查看一下我们电脑的主板设置中U盘启动的类型是什么,是UEFI还是legacy? 对于如果是UEFI那么给ubuntu分区的时候不用设置/boot分区,设置efi系统分区:如果是 ...

  10. 用python代码玩微信

    # 安装包 pip install -U wxpy from wxpy import * import time import json bot=Bot() my_friend = bot.frien ...