hdoj 3709 Beautiful numbers
来源:互联网 发布:java官网网址 编辑:程序博客网 时间:2024/06/05 04:08
题目链接:Balanced Number
题目大意:给你n和m,问你这个范围里面的平衡数有多少,平衡数是指以某一个数位位对称轴,两边距离乘数位算贡献,如果相等,则位平衡数
题目思路:枚举对称轴,数位dp,算距离不取绝对值,小于零剪枝,需要注意的是0,00,000这样的数都不是平衡数,但是数位dp的时候我们枚举进去了,所以需要减去
#include <map>#include <set>#include <queue>#include <stack>#include <cmath>#include <vector>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;ll digit[20],dp[20][5000][20];ll dfs(ll pos,ll num,ll pivot,bool limit){ if(pos == -1) return num == 0?1:0; if(num < 0) return 0; if(!limit&&dp[pos][num][pivot] != -1) return dp[pos][num][pivot]; ll up = limit?digit[pos]:9; ll ans = 0; for(ll i = 0;i <= up;i++){ ans += dfs(pos-1,num+i*(pos-pivot),pivot,limit&&i == digit[pos]); } if(!limit) dp[pos][num][pivot] = ans; return ans;}ll solve(ll x){ ll pos = 0; ll res = 0; if(x == -1) return 0; while(x){ digit[pos++] = x%10; x/=10; } for(ll i = 0;i < pos;i++) res += dfs(pos-1,0,i,true); return res-pos+1;}int main(){ ll n,m,t; memset(dp,-1,sizeof(dp)); scanf("%lld",&t); while(t--){ scanf("%lld%lld",&n,&m); printf("%lld\n",solve(m)-solve(n-1)); } return 0;}
阅读全文
0 0
- hdoj 3709 Beautiful numbers
- Beautiful Numbers
- Beautiful numbers
- Beautiful Numbers
- Beautiful numbers
- Beautiful numbers
- Why are numbers beautiful?
- Codeforces_C - Beautiful Numbers
- codeforces Beautiful Numbers
- UVa 11472 - Beautiful Numbers
- codeforces55D Beautiful numbers
- codeforces300C. Beautiful Numbers
- C. Beautiful Numbers
- E - Beautiful Numbers
- 数位dp Beautiful numbers
- 数位dp, Beautiful numbers
- Beautiful numbers CodeForces
- Beautiful numbers CodeForces
- 大型网站技术架构(七)--网站的可扩展性架构
- URL和URI区别
- OpenCV Mat访问指定像素值
- android eclipse版七牛最新的7.3.9 library 引入工程项目
- 音频的采集和播放
- hdoj 3709 Beautiful numbers
- ImportError: No module named '_pywrap_tensorflow_internal'解决方法
- 掌握Linux系统后3个月内找到一份9K运维工作
- 大型网站技术架构(八)--网站的安全架构
- Jobdu1456 胜利大逃亡 (BFS,三维空间)
- kali linux on android armhf 安装桌面
- HSV、RGB颜色空间范围取值的确定
- POJ 3252Round Numbers
- jsonp的封装