博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU3709:Balanced Number(数位dp)
阅读量:6413 次
发布时间:2019-06-23

本文共 2046 字,大约阅读时间需要 6 分钟。

Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5097    Accepted Submission(s): 2434


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 ≤ 10
18).
 

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
 

Author
GAO, Yuan
 

Source
题意:求范围内的平衡数个数,平衡数即以某个数位为支点,其左边的数字乘以对应力矩的和等于右边数字的力矩和。

思路:枚举支点位置即可。

# include 
# include
# define LL long longint a[20];LL dp[19][19][2000];LL dfs(int pos, int piv, int l, bool limit)//数位,支点位置,当前力矩和,上界限制{ if(pos==-1) return l==0; if(!limit && dp[pos][piv][l] != -1) return dp[pos][piv][l]; if(l<0) return 0; int up = limit?a[pos]:9; LL tmp = 0; for(int i=0; i<=up; ++i) { long long tmp2 = l; tmp2 += (pos-piv)*i; tmp += dfs(pos-1, piv, tmp2, limit&&i==a[pos]); } if(!limit) dp[pos][piv][l] = tmp; return tmp;}LL solve(LL num){ int cnt = 0; LL ans = 0; while(num) { a[cnt++] = num%10; num /= 10; } for(int i=0; i

转载于:https://www.cnblogs.com/junior19/p/6730032.html

你可能感兴趣的文章
spring boot + mybatis 同时访问多数据源
查看>>
Linux服务器(CentOS)安装SVN(subversion)教程
查看>>
Oracle官网下载jdk 版本
查看>>
URL中汉字转码
查看>>
搭建TurnServer服务器
查看>>
转载:PHP性能提升之OPcache相关参数详解
查看>>
[转]Tutorial about USB HID Report Descriptors
查看>>
方法重写或者方法覆盖
查看>>
[转]go正则实例
查看>>
Selector中关于顺序的注意事项
查看>>
font-size: 62.5% 的含义
查看>>
MapXtreme 2005 GIS开发入门系列 索引
查看>>
小黑小波比.清空<div>标签内容
查看>>
Java中的ExceptionInInitializerError异常及解决方法
查看>>
Spring 注入bean时的初始化和销毁操作
查看>>
java线程同步原理(lock,synchronized)
查看>>
MyEclipse中使用Hql编辑器找不到Hibernate.cfg.xml文件解决方法
查看>>
yRadio以及其它
查看>>
第四节 对象和类
查看>>
apkplug主题皮肤切换之通用主题-04
查看>>