Balanced Number

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

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
int digit[];
__int64 dp[][][];
__int64 dfs(int pos,int c,int l,int lim)
{
if(pos==) return l==;
if(l<) return ;
if(!lim && dp[pos][c][l]!=-) return dp[pos][c][l];
int n=lim?digit[pos]:;
__int64 ans=;
for(int i=; i<=n; i++)
{
int next=l;
next+=(pos-c)*i;
ans+=dfs(pos-,c,next,lim&&i==n);
}
if(!lim) dp[pos][c][l]=ans;
return ans;
}
__int64 solve(__int64 n)
{
int len=;
while(n)
{
digit[++len]=n%;
n/=;
}
__int64 sum=;
for(int i=; i<=len; i++) sum+=dfs(len,i,,);
return sum-len+;
}
int main()
{
int t;
__int64 x,y;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--)
{
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",solve(y)-solve(x-));
}
return ;
}

另一个带注释的AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int bit[];
__int64 dp[][][];
//pos为当前位置
//o为支点
//l为力矩
//work为是否有上限
__int64 dfs(int pos,int o,int l,int work)
{
if(pos==-) return l==;//已经全部组合完了
if(l<) return ;//力矩和为负,则后面的必然小于0
if(!work && dp[pos][o][l]!=-) return dp[pos][o][l];//没有上限,且已经被搜索过了
__int64 ans=;
int end=work?bit[pos]:;//有上限就设为上限,否则就设为9
for(int i=; i<=end; i++)
{
int next=l;
next+=(pos-o)*i;//力矩
ans+=dfs(pos-,o,next,work&&i==end);
}
if(!work) dp[pos][o][l]=ans;
return ans;
}
__int64 solve(__int64 n)
{
int len=;
while(n)
{
bit[len++]=n%;
n/=;
}
__int64 ans = ;
for(int i=; i<len; i++) ans+=dfs(len-,i,,);
return ans-(len-);//排除掉0,00,000....这些情况
} int main()
{
int T;
__int64 l,r;
scanf("%d",&T);
memset(dp,-,sizeof(dp));
while(T--)
{
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",solve(r)-solve(l-));
}
return ;
}

hdu3709 Balanced Number (数位dp+bfs)的更多相关文章

  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

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

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

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

  4. HDU 3709 Balanced Number (数位DP)

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

  5. hdu3709 Balanced Number 树形dp

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

  6. Balanced Number 数位dp

    题意: 给出求ab之间有多少个平衡数   4139为平衡数   以3为轴   1*1+4*2==9*1 思路很好想但是一直wa  : 注意要减去前导零的情况 0 00 000 0000   不能反复计 ...

  7. [HDU3709]Balanced Number

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

  8. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  9. HDU3709 Balanced Number (数位dp)

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

随机推荐

  1. codeforces 425B Sereja and Table (枚举、位图)

    输入n*m的01矩阵.以及k. n,m<=100,k<=10 问修改至多k个,使得矩阵内的各连通块(连着的0或1构成连通块)都是矩形,且不含另外的数字(边界为0(1)的矩形内不含1(0)) ...

  2. 【文件】读取一个文件夹下所有的jpg图片

    今天做视频处理的时候,发现给的视频是用jpg图片的形式给出的,名字的命名规律性不是很强.就想找一种通用的遍历文件夹下图片的方法. 开始在网上找到了下面这份代码,发现只能读取所有的文件夹,文件都被跳过了 ...

  3. CCF 节日

    问题描述 有一类节日的日期并不是固定的,而是以"a月的第b个星期c"的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ ...

  4. 创建odoo数据库时出现错误原因

    安装完odoo 8.0后创建数据库时出现如下错误信息: Odoo Odoo Server Error Traceback (most recent call last): File "D:\ ...

  5. hud 2602 Bone Collector

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 思路:典型的01背包 #include<stdlib.h> #include< ...

  6. windows服务 2.实时刷新App.config

    参考 http://www.cnblogs.com/jeffwongishandsome/archive/2011/04/24/2026381.html http://www.cnblogs.com/ ...

  7. Oracle 数组赋值

    只需要像下面这样就OK了 begin -- Call the procedure in_var(1):=null;in_var(1):='a123123'; pack_abc.pro_abc(in_v ...

  8. go sample-base64

    GoSample-base64 package mainimport ( "encoding/base64" "fmt")func base64Encode(s ...

  9. linux系统定时任务

    crontab常用的几个命令如下 sudo crontab -l #显示所有的定时任务 sudo crontab -e #编辑任务 sudo crontab -r #删除所有的任务 编辑任务时的书写方 ...

  10. iOS中图片动画的三种模式及基本的代码实现

    -(void)play { //第一种图片动画模式 头尾方式 //头尾方式 [UIView beginAnimations:nil context:nil];//动画开始 [UIView setAni ...