hdu 3709 Balanced Number(数位dp)
Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4624 Accepted Submission(s): 2171
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
2
0 9
7604 24324
Sample Output
10
897
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /* hdu 3709 Balanced Number(数位dp) problem: 求[a,b]平衡数的个数.(以这个数的某一位为支点,另外两边的数字大小乘以力矩). solve: 可以枚举支点位置,然后数位dp计算力矩和为0的个数 hhh-2016-08-24 19:54:09 */ #pragma comment(linker,"/STACK:124000000,124000000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <vector> #include <math.h> #include <queue> #include <map> #define lson i<<1 #define rson i<<1|1 #define ll long long #define clr(a,b) memset(a,b,sizeof(a)) #define scanfi(a) scanf("%d",&a) #define scanfl(a) scanf("%I64d",&a) #define key_val ch[ch[root][1]][0] #define inf 0x3f3f3f3f #define mod 1000003 using namespace std; const int maxn = 100010; ll dp[20][20][2005]; int t[20]; ll dfs( int len, int o, int all, int flag) { if (len < 0) return all == 0; if (all < 0) return 0; if (dp[len][o][all] != -1 && !flag) return dp[len][o][all]; int n = flag ? t[len]:9; ll ans = 0; for ( int i = 0;i <= n;i++) { int ta = all; ta += (len - o) * i; ans += dfs(len-1,o,ta, flag && i == n); } if (!flag) dp[len][o][all] = ans; return ans; } ll cal(ll b) { int tot = 0; while (b) { int p = b % 10; t[tot++] = p; b /= 10; } ll ans = 0; for ( int i = 0; i <tot; i++) ans += dfs(tot-1,i,0,1); // cout <<ans <<endl; return ans-(ll)(tot-1); } int main() { int T; ll a,b; // freopen("in.txt","r",stdin); scanfi(T); memset (dp,-1, sizeof (dp)); while (T--) { scanfl(a),scanfl(b); // cout << a <<" " <<b <<endl; printf ( "%I64d\n" ,cal(b) - cal(a-1)); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· 盘点!HelloGitHub 年度热门开源项目
· 某Websocket反爬逆向分析+请求加解密+还原html
· 02现代计算机视觉入门之:什么是视频
· 回顾我的软件开发经历:我与代码生成器的涅槃之路
· DeepSeek V3 两周使用总结