数位dp(平衡数)
来源:互联网 发布:100兆访客网络限速 编辑:程序博客网 时间:2024/06/11 07:55
hdu3709 Balanced Number
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
题意:找出区间[x,y](0<=x<=y<=10^18)内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数
暴力是会炸的哟!
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;__int64 len,wei[22];__int64 dp[22][22][2005];__int64 dfs(__int64 pos,__int64 o,__int64 pre,__int64 flag){//flag代表当前数字有没有上限 if(pos==0){//已经遍历完每一位的时候 if(pre==0)return 1; return 0; } if(pre<0)return 0; if(flag==0&&dp[pos][o][pre]!=-1)return dp[pos][o][pre]; __int64 ans=0; __int64 ed=flag?wei[pos]:9; for(__int64 i=0;i<=ed;i++){ __int64 next=pre; next+=(pos-o)*i;//算出下一步的力矩和 ans+=dfs(pos-1,o,next,flag&&i==ed); } if(!flag)dp[pos][o][pre]=ans;//如果当前没有上限,保存dp值 return ans;}__int64 solve(__int64 x){ if(x==-1)return 0; if(x==0)return 1; __int64 t=x; len=0; while(t){ wei[++len]=t%10; t/=10; } cout<<len<<endl; __int64 ans=0; for(int i=len;i>=1;i--){ ans+=dfs(len,i,0,1);//以i为支点寻答案 } ans-=(len-1);//减去全是0的情况 return ans;}int main(){ __int64 t,l,r; memset(dp,-1,sizeof(dp)); scanf("%I64d",&t); while(t--){ scanf("%I64d%I64d",&l,&r); printf("%I64d\n",solve(r)-solve(l-1)); } return 0;}
阅读全文
1 0
- 数位dp(平衡数)
- hdu3709(求区间内平衡数的个数)数位dp
- windy数(数位DP)
- windy数(数位dp)
- Windy数(数位dp)
- Windy数(数位dp)
- hdu3709 数位dp(自身平衡的数字)
- 蓝桥杯 K好数(数位DP)
- BZOJ1026 [SCOI2009]windy数(数位dp)
- BZOJ 1026 windy数 (数位DP)
- bin巨的数(数位DP)
- UESTC 250 windy数(数位DP)
- UESTC 250 windy数 (数位DP)
- ccf 有趣的数(数位dp)
- [BZOJ1026][SCOI2009]windy数(数位dp)
- HYSBZ - 1026 windy数 (数位DP)
- BZOJ-1026-windy数(数位DP)
- uestc1307 windy数 (数位DP)
- 【过滤器】(2)过滤器的应用案例:网站安全登录技术
- 前端之盒子类重力感应拖拽效果
- redis
- CSS3选择器
- springboot(十三):springboot小技巧
- 数位dp(平衡数)
- 用python写网络爬虫-下载百思不得姐视频
- 查看SELinux状态及关闭SELinux
- 1059. C语言竞赛
- 老鹰牛顿(Newton)宝典教学 视频教学
- linux下的IPC通信机制
- 维修工程师不容易
- JavaScript基础二
- 剑指offer--连续子数组的最大和