HDU3709 Balanced Number (数位dp)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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
数位dp
表示暂时还不明白为什么这样搜索不会超时。。
#include<cstdio>
#include<cstring>
long long f[][][];
int a[];
long long dfs(int pos,int z,int l,bool pd){
if(pos<=) return l==;
if(l<) return ;
if(!pd&&f[pos][z][l]!=-) return f[pos][z][l];
int i,j,k,en;
en=pd?a[pos]:;
long long ans=;
for(i=;i<=en;i++)
ans+=dfs(pos-,z,l+(pos-z)*i,pd&&i==en);
if(!pd) f[pos][z][l]=ans;
return ans;
}
long long sum(long long x){
int i,n=;
while(x){
a[++n]=x%;
x=x/;
}
long long ans=;
for(i=n;i>;i--)
ans+=dfs(n,i,,);
return ans-n+;
}
int main()
{
int tt;
long long x,y;
scanf("%d",&tt);
while(tt--){
scanf("%lld%lld",&x,&y);
memset(f,-,sizeof(f));
printf("%lld\n",sum(y)-sum(x-));
}
return ;
}
HDU3709 Balanced Number (数位dp)的更多相关文章
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- 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
题目链接: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 用作标记,当现在枚举的数小 ...
随机推荐
- Java 8 VM GC Tuning Guide Charter3-4
第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...
- 兼容iOS 10 资料整理笔记-b
原文链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化 ...
- select 取值
select 标签是下拉列表标签.在网站中使用很多. 在后台服务器标记的下拉框在前台都会生成select标签. 他的子项就是option标签.要确认一个项是否被选中,就要确定子项中的selected标 ...
- HIVE Transform using 用法
select TRANSFORM(*, *, *) using 'python filter.py' as (*, *, *) from t_1 HIVE支持pipe操作,将select出来的字段,用 ...
- ViewController 优化
解决问题:部分复杂页面的Controller过于庞大,不利于维护与复用: 复杂的页面大多是基于tableview的页面.复杂页面的代码大致可分为两部分(复杂的View布局用Nib实现的话,一般大家都是 ...
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
- JS Web打印,实现预览新样式
问题描述: JS实现Web打印,要求打印前一种样式,打印预览时新样式 问题解决: (1)设置打印时的css样式,设置打印前的css样式 注: 以上为print. ...
- pureftpd安装配置-pureftp参数详解(一)
1. 下载 #cd /usr/local/src/ #wget ftp://ftp.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.30.tar.g ...
- uva 10771
思路题 K的人数只能以2减少 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...
- Nginx配置一个自签名的SSL证书
http://www.liaoxuefeng.com/article/0014189023237367e8d42829de24b6eaf893ca47df4fb5e000 要保证Web浏览器到服务器的 ...