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. linux下修改tomcat内存大小

    转载自:http://blog.sina.com.cn/s/blog_7fb5109d0101o49a.html 1.  出现此问题的原因:       在tomcat中发布了多个webapp引用,每 ...

  2. Mysql 基础1

    Mysql int 整型float 小数double 小数varchar(20) 字符串bit 布尔型数据datetime 日期时间类型text 长文本 money 存货币image 存二进制数据 数 ...

  3. [页面滚动到底部]jquery $(window).height()取值等于$(document).height()的问题

    问题现象:JSP中头部引用了某个head.jsp,在videoList.jsp中生成片段时如下 实际最终生成的HTML如下: <!DOCTYPE html>没有解析到,原因找到了,先想办法 ...

  4. 使用WKWebView替换UIWebView

    开发App的过程中,常常会遇到在App内部加载网页,通常用UIWebView加载.这个自iOS2开始使用的网页加载器一直是开发的心病:加载速度慢,占用内存多,优化困难.如果加载网页多,还可能因为过量占 ...

  5. iOS push与present Controller的区别

    push与present都可以推出新的界面.present与dismiss对应,push和pop对应.present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根vc,其他 ...

  6. GridView如何合并同类项

    /// <summary>     /// 合并GridView中某列相同信息的行(单元格)     /// </summary>     /// <param name ...

  7. Redis内存管理(一)

    Redis数据库的内存管理函数有关的文件为:zmalloc.h和zmalloc.c. Redis作者在编写内存管理模块时考虑到了查看系统内是否安装了TCMalloc或者Jemalloc模块,这两个是已 ...

  8. linux crontab 学习

    安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum install crontabs/sbin/service ...

  9. 使用Gson送解析Json格式

    Java bean: package com.jingle.a; public class Person { public String name; public int age; public Pe ...

  10. Foreign key (referential) constraints on DB2 LUW v105

    oreign key constraints (also known as referential constraints or referential integrity constraints) ...