hdoj 3709 Balanced Number 【数位dp】
来源:互联网 发布:php 数值相加 编辑:程序博客网 时间:2024/06/18 07:17
Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3353 Accepted Submission(s): 1527
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].
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
20 97604 24324
Sample Output
10897
定义n是一个平衡数:存在一个平衡点mid,使得左右力矩相同。如4139,3是平衡点,4*2+1*1 == 9*1。
题意:问你[L, R]里面的平衡数个数。
思路:也算很裸额数位dp了,枚举平衡点就可以了。设置dp[i][sum][mid] 前i位力矩和为sum,平衡点为mid的方案数。不同的是需要特别处理全0的情况,因为DFS中zero不能去掉多个0的情况。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 1000000#define eps 1e-8#define MAXN (200000+10)#define MAXM (100000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;typedef pair<int, int> pii;LL dp[30][2000][30];int bit[30];LL DFS(int pos, int sum, int mid, bool yes, bool zero){ if(pos == -1) return sum == 0; if(sum < 0) return 0; if(!yes && dp[pos][sum][mid] != -1) return dp[pos][sum][mid]; LL ans = 0; int End = yes ? bit[pos] : 9; for(int i = 0; i <= End; i++) ans += DFS(pos-1, zero&&i==0 ? 0 : sum+i*(pos-mid), mid, yes&&i==End, zero&&i==0); if(!yes) dp[pos][sum][mid] = ans; return ans;}LL Count(LL n){ int len = 0; while(n) { bit[len++] = n % 10; n /= 10; } LL ans = 0; for(int i = 0; i < len; i++) ans += DFS(len-1, 0, i, 1, 1); return ans-(len-1);}int main(){ CLR(dp, -1); int t; Ri(t); W(t) { LL n, m; Rl(n); Rl(m); Pl(Count(m) - Count(n-1)); } return 0;}
0 0
- hdoj 3709 Balanced Number 【数位dp】
- Balanced Number [数位DP]
- HDU 3709 Balanced Number(数位DP)
- 数位dp HDU 3709 Balanced Number
- hdu 3709 Balanced Number (数位dp)
- hdu 3709 Balanced Number(数位dp)
- Balanced Number - HDU 3709 数位dp
- [数位dp] hdu 3709 Balanced Number
- 【HDU】3709 Balanced Number 数位DP
- HDU 3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number(数位DP)
- 【数位DP】 HDU 3709 Balanced Number
- hdu 3709Balanced Number(数位dp)
- HDU 3709 Balanced Number (简单数位DP)
- HDU 3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number (数位dp)
- 数位DP-HDU-3709-Balanced Number
- HDU 3709 Balanced Number(数位dp)
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- tar 命令解析 .tar .gz .bz2
- DMA映射
- Linux 查看内存插槽数、最大容量和频率
- 架设Squid代理作为WEB缓存加速
- hdoj 3709 Balanced Number 【数位dp】
- Hexo+GitHub博客
- CSS属性之box-reflect(倒影)
- 8939sbl部分关闭LDO6电源延迟120ms之后再打开
- Nsdate 的扩展
- RtlFreeUnicodeString routine
- android用数组保存输出流的数据包的问题(最后等到的所有的都变成最后一个包)
- MySQL 入门 之 表 的 创建、修改与删除
- Vim常用命令:移动 跳转 到 文档开头或末尾