Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 9036    Accepted Submission(s): 4294

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
分析:对于这道题,如果一个数中每个数位到支点的距离*这个数位的和为0,那么这个数为平衡数.这样我们定义状态就要考虑力矩和和支点.支点可以在dfs前枚举得到,力矩和可以在处理每个数位的时候得到.但是这个算法是有缺陷的,例如0000,000000也会被统计,我们只需要减去给定范围0全是0的数的个数即可.这里可以进行一个小小的优化,如果力矩和已经为负数,说明已经处理到了支点左边,接着处理下去绝对会小于0,那么回溯即可
 
#include<iostream>
#include<string.h>
#define ll long long
using namespace std;
ll shu[], dp[][][];//dp[i][j][k],i是长度,j是支点,k是力矩和,dp[i][j][k]是以j为支点的平衡数的数量
ll dfs(ll len, ll zhidian, ll sum, bool shangxian)
{
if (len == )
return sum == ? : ;
if (sum < )//因为力矩和是从支点右边开始算的(sum>0),如果左边的力矩都处理完之后sum<0,那一定不是平衡数
return ;
if (!shangxian&&dp[len][zhidian][sum]!=-)
return dp[len][zhidian][sum];
ll mx, cnt = ;
mx = (shangxian ? shu[len] : );
for (ll i = ; i <= mx; i++)//注意是<=
{
ll temp = sum;
temp = temp + (len - zhidian)*i;//len会不断的被return ,细细体会
cnt = cnt + dfs(len - , zhidian, temp, i == mx && shangxian);
}
if (!shangxian)
dp[len][zhidian][sum] = cnt;
return cnt;
} ll solve(ll n)
{
ll len = ;
while (n)
{
shu[++len] = n % ;
n = n / ;
}
ll ans = ;
for (ll i = ; i <= len; i++)//支点是从1开始,因为最高位的数一定不是平衡数
ans = ans + dfs(len, i, , true);
return ans - (len - );//如果0是支点,程序也会判断是平衡数,但是不符合题意
}
int main()
{
ll l, r, t;
scanf("%lld", &t);
memset(dp,-,sizeof(dp));//如果默认0会TLE
while (t--)
{
scanf("%lld%lld", &l, &r);
printf("%lld\n", solve(r) - solve(l - ));
}
return ;
}

参考自https://www.cnblogs.com/zbtrs/p/6106783.html

hdu 3709 Balanced Number(平衡数)--数位dp的更多相关文章

  1. HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)

    平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...

  2. HDU 3709 Balanced Number (数位DP)

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

  3. hdu 3709 Balanced Number(数位dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:给定区间[a,b],求区间内平衡数的个数.所谓平衡数即有一位做平衡点,左右两边数字的力矩相 ...

  4. HDU 3709 Balanced Number(数位DP)题解

    思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...

  5. HDU 3709 Balanced Number (数位DP)

    题意: 找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数. 思路: 一开始以为需要枚举位数,枚举前缀和,枚举后缀和,一旦枚举起来就会M ...

  6. HDU - 3709 - Balanced Number(数位DP)

    链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...

  7. hdu3709 (平衡数) 数位DP

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

  8. HDU 3709 Balanced Number

    发现只要Σa[i]*i%Σa[i]==0就可以. #include<iostream> #include<cstdio> #include<cstring> #in ...

  9. SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

    题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...

随机推荐

  1. W-D-S-DDR

    要把下载到nandflash里面的程序(大于8KB的时候)拷贝到链接地址,故要初始化DDR,才能够使用DDR. ??? 开发板上电后要初始化DRAC,以及DDR,然后把程序拷贝到50000000出运行 ...

  2. ie高版本浏览器不支持velocity的问题解决

    <head><meta http-equiv="X-UA-Compatible" content="IE=5"></head> ...

  3. tomcat启动了server.xml中没有配置的项目

    在tomcat的conf目录下的server.xml文件中没有配置hczm_struts项目,但在eclipse启动tomcat调试时,一直启动hczm_struts项目. 经检查,发现conf\Ca ...

  4. SpringMvc 全局异常处理器定义,友好的返回后端错误信息

    import com.google.common.collect.Maps; import org.apache.log4j.Logger; import org.springframework.be ...

  5. 【工具推荐】ELMAH——可插拔错误日志工具(转)

    出处:http://www.cnblogs.com/liping13599168/archive/2011/02/23/1962625.html 今天看到一篇文章(构建ASP.NET网站十大必备工具( ...

  6. oracle 11g 创建普通用户

    CREATE USER xiaoming IDENTIFIED by xm123123 GRANT CREATE SESSION TO xiaoming; GRANT RESOURCE TO xiao ...

  7. 企业搜索引擎开发之连接器connector(十七)

    本文描述连接器的提供与外界交互的servlet接口,连接器与外部是通过xml格式数据交互的 1)  获取所有连接类型 提交地址:http://localhost:8080/connector-mana ...

  8. C#基础入门 十

    C#基础入门 十 Windows应用程序的界面设计 Form.cs:窗体文件,一般用于存放程序员为窗体编写的代码: Form.Designer.cs:窗体设计文件,其中的代码是由VS自动生成的,一般不 ...

  9. [C#学习笔记]分部类和分部方法

    知识在于积累. 前言 好久没写博客了,因为在看<CLR via C#>的时候,竟然卡在了分部方法这一小节几天没下去.今天重新认真阅读,有些感悟,所以在此记录. 然后. 每天早晨第一句,&l ...

  10. 【SSH学习笔记】用Struts2实现简单的用户登录

    准备阶段 在使用学习Struts2的时候首先要下载相应的架包 Struts2资源下载 这里建议下载第一个,在struts-2.5.14.1-all.zip里有很多实用的东西,不仅有架包还有官方为开发者 ...