hdu3709 Balanced Number
来源:互联网 发布:java递归 简单例子 编辑:程序博客网 时间:2024/05/17 07:16
Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1353 Accepted Submission(s): 591
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
10897dp[i][j][k]表示有i位时,第j个数字做为中心,最后算得的权重为k,再用一下记忆化搜索就可以了,对于任意一个平衡数,一定只有一个中心使得其为平衡数,所以就可以枚举中心搜索,但是这样,会产生0,00,000,000...等这样的数,要最后减去就可以了!#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define M 23__int64 pri[M],dp[M][M][2050];__int64 dfs(int pos,int cen,__int64 pre,int flag){ if(pos==0)return pre==0; if(pre<0)return 0; if(!flag&&dp[pos][cen][pre]!=-1)return dp[pos][cen][pre]; int u=flag?pri[pos]:9;__int64 ans=0; for(int i=0;i<=u;i++) ans+=dfs(pos-1,cen,pre+(__int64)(i*(pos-cen)),flag&&i==u); return flag?ans:dp[pos][cen][pre]=ans;}__int64 solve(__int64 x){ int cnt=0; while(x){ pri[++cnt]=x%10;x/=10; } __int64 ans=0; for(int i=cnt;i>0;i--) ans+=dfs(cnt,i,0,1); return ans-cnt+1;}int main(){ int tcase;__int64 n,m; scanf("%d",&tcase); while(tcase--){ scanf("%I64d%I64d",&n,&m); memset(dp,-1,sizeof(dp)); printf("%I64d\n",solve(m)-solve(n-1)); } return 0;}
- hdu3709 Balanced Number
- hdu3709 Balanced Number
- hdu3709 Balanced Number
- 【HDU3709】【Balanced Number】
- hdu3709: Balanced Number
- hdu3709 Balanced Number
- 【HDU3709】 Balanced Number
- HDU3709 Balanced Number
- HDU3709 Balanced Number 数位DP
- [HDU3709]Balanced Number && 数位DP
- hdu3709 Balanced Number 数位dp
- hdu3709---Balanced Number(数位dp)
- hdu3709 Balanced Number 数位dp
- 数位dp hdu3709 Balanced Number
- HDU3709 Balanced Number 数位DP
- hdu3709 Balanced Number 数位dp
- hdu3709——Balanced Number
- HDU3709 Balanced Number[数位DP]
- 螺旋矩阵
- 怎样给领导汇报工作
- 参考文献中常见的会议论文的出版者和出版地 (
- 数据表合并1
- QoE的定义及影响因素
- hdu3709 Balanced Number
- HUNNU--湖师大--11409--Skill
- TWU——Life in TWU
- Javascript+HTML若干注意事项
- 数据表合并2
- 《秘密》观后感!
- 闪回版本查询与闪回事务查询(Flashback Version Query、Flashback Transaction Query)
- 盘古开天地
- Java 技术: 使您轻松地进行多线程应用程序编程——Consumer 类可以简化生产者-消费者行为的实现