【HDU 3709】 Balanced Number (数位DP)
Balanced Number
Problem DescriptionA 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 ≤ 1018).OutputFor each case, print the number of balanced numbers in the range [x, y] in a line.Sample Input2
0 9
7604 24324Sample Output10
897AuthorGAO, YuanSource
  一开始纠结的地方呢就是一个数可不可能有两个平衡点。。然后就觉得会重复。
      用物理的思想想的话好像一个物体又没有两个重心??可是我不会用数学方法严谨证明ORZ。。。
      然后就是普通的数位DP,f[i][j][k]表示填i位j是平衡点,然后k是当前的计算值。(不超过4000)
      记忆化搜索大法好,又快又好打,特别是多组!!
记得减掉00、000、0000的!
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long LL f[][][];
int a[],c; LL ffind(int n,int k,int sum,bool flag)
{
if(n==) return (sum==);
if(!flag&&f[n][k][sum]!=-) return f[n][k][sum];
int ed=flag?a[n]:;
LL ans=;
for(int i=;i<=ed;i++)
ans+=ffind(n-,k,sum+i*(k-n),flag&&(i==ed));
if(!flag) f[n][k][sum]=ans;
return ans;
} LL get_ans(LL n)
{
if(n==-) return ;
if(n==) return ;
c=;
LL x=n;
while(x) a[++c]=x%,x/=;
LL ans=;
for(int i=;i<=c;i++) ans+=ffind(c,i,,);
return ans-c+;
} int main()
{
int T;
scanf("%d",&T);
memset(f,-,sizeof(f));
while(T--)
{
LL x,y;
scanf("%lld%lld",&x,&y);
get_ans();
LL ans=get_ans(y)-get_ans(x-);
printf("%lld\n",ans);
}
return ;
}
[HDU 3709]
感觉我没有判负就A了?!!
2016-10-08 20:31:48
【HDU 3709】 Balanced Number (数位DP)的更多相关文章
- HDU 3709 Balanced Number (数位DP)
		
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
 - hdu 3709 Balanced Number(平衡数)--数位dp
		
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
 - HDU 3709 Balanced Number(数位DP)题解
		
思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...
 - HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
		
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...
 - HDU - 3709 - Balanced Number(数位DP)
		
链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...
 - hdu 5898 odd-even number 数位DP
		
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
 - HDU 5787 K-wolf Number 数位DP
		
K-wolf Number Problem Description Alice thinks an integer x is a K-wolf number, if every K adjacen ...
 - hdu3709 Balanced Number (数位dp+bfs)
		
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
 - HDU3709:Balanced Number(数位DP+记忆化DFS)
		
Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...
 - HDU 5179 beautiful number 数位dp
		
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
 
随机推荐
- 深入理解计算机系统第二版习题解答CSAPP 2.6
			
使用打印字节的方式可以知道十进制数12345的十六进制为0x00003039,十进制浮点数12345.0的十六进制为0x4640E400. 转换为二进制为 /* 0 0 0 0 3 0 3 9 *** ...
 - inux设置普通用户无密码sudo权限
			
配置普通用户无密码sudo权限: root用户进入到Linux系统的/etc目录下 cd /etc 将sudoers文件赋予写的权限 chmod u+w /etc/sudoers 编辑sudoers文 ...
 - Socket.io各个发送消息的含义
			
// send to current request socket client socket.emit('message', "this is a test"); // send ...
 - linux学习记录(第六章、Linux 的文件权限与目录配置)
			
书看的是鸟哥的私房菜,系统用的是centos.被微软坑了N年才发现linux才是王道. 在这里记录些学习的记录.备忘
 - datetimepicker 初始化只显示年
			
$("#overdue2").datetimepicker({ format: 'yyyy', autoclose: true, startView:4, minView:4, t ...
 - eclipse上传显示svn上传者名
			
这里来记录下eclipse上传显示svn上传者名. 如图所示,修改即可.
 - asp.net 开发问题:Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值。
			
"Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值." 这个问题在开发需要上传文件的时候可能会遇到,今天遇到这个问题,百度过也有挺多的修改方法. 方法1: 修 ...
 - iis的路径
			
每次打开iis管理器查看iis指定路径下的文件过于麻烦,而且iis管理器耗资源,以下是iis的路径,以及其对应在本地磁盘的地址 SP2013\Sites\SharePoint - 80\_contro ...
 - ASP.NET CompareValidator 控件在VS2012中出错的问题
			
CompareValidator 控件用于将由用户输入到输入控件的值与输入到其他输入控件的值或常数值进行比较. -------如果输入控件为空,则不会调用任何验证函数,并且验证将成功.使用 Requi ...
 - 如何关闭SELinux
			
一般安装linux课程时都把SELinux与iptables安排在后面,使初学者配置linux服务器时不成功,却没有头绪,那是因为在RedHat linux操作系统中默认开启了防火墙,SELinux也 ...