hdu3709 Balanced Number (数位dp+bfs)
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)的更多相关文章
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- hdu3709 Balanced Number 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...
- 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 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu3709 Balanced Number 树形dp
A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...
- Balanced Number 数位dp
题意: 给出求ab之间有多少个平衡数 4139为平衡数 以3为轴 1*1+4*2==9*1 思路很好想但是一直wa : 注意要减去前导零的情况 0 00 000 0000 不能反复计 ...
- [HDU3709]Balanced Number
[HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...
- 多校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 用作标记,当现在枚举的数小 ...
- HDU3709 Balanced Number (数位dp)
Balanced Number Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
随机推荐
- 安装PostgreSQL数据库 ,Database Cluster 失败!
在安装PG数据库的过程中,会选择安装目录以及数据存放目录和端口,并需要选择Local,如果全部使用默认,并且设置好自己的密码后开始安装,前期进展还比较顺利,到了安装Database Cluster时, ...
- DB2用一张表更新其他表的数据
表结构: CREATE TABLE ATEST (ID INTEGER, NAME VARCHAR(256), CODE INTEGER, NAME2 VARCHAR(256) ...
- WCF服务跟踪
如果在开发过程中,WCF服务出现问题,我们可以通过服务引用,然后直接断点调试进去.然而,对于已经发布的服务,出现错误时,寻找错误信息会变得麻烦. 幸好,微软提供了服务跟踪查看器工具 (SvcTrace ...
- 【文件】读取一个文件夹下所有的jpg图片
今天做视频处理的时候,发现给的视频是用jpg图片的形式给出的,名字的命名规律性不是很强.就想找一种通用的遍历文件夹下图片的方法. 开始在网上找到了下面这份代码,发现只能读取所有的文件夹,文件都被跳过了 ...
- 【Git】笔记5 分支管理2
来源:廖雪峰 通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一 ...
- supersr--图形上下文的注意点
- (void)test { // 不要自己调用drawRect:方法的原因: // 当系统调用drawRect:方法之前, 会创建一个与当前UIView的layer相关的图形上下文, 这样就可以保证 ...
- jquery.validation.js 表单验证
jquery.validation.js 表单验证 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuer ...
- route命令
Linux系统的route 命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器, ...
- NYOJ题目10505C?5S?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJ/CAIAAAAbDelhAAAgAElEQVR4nO3dPXLbOhfG8XcT7r0Q11
- Android Programming: Pushing the Limits -- Chapter 5: Android User Interface Operations
多屏幕 自定义View 多屏幕 @.Android 4.2 开始支持多屏幕. @.举例: public class SecondDisplayDemo extends Activity { priva ...