HDOJ 3709 balanced number (digit dp)
来源:互联网 发布:淘宝平均停留时间 编辑:程序博客网 时间:2024/06/08 03:10
link to the question
I spend almost a whole afternoon to figure this question out and I did it! I made annotation right after the code to help understand. Although I use some else’s code for reference , I think I ,now,understand how to solve the questions alike.
#include<cstdio>#include<cstring>#include<iostream>#include<string>//d[pos][sum][axis]using namespace std;__int64 dp[20][30][2050];__int64 a[20];__int64 dfs(int pos,int sum,int axis,bool limit){ if(pos==0) {return sum == 0;} //whether the number is acceptable!!!! if(sum<0) return 0; //edge_cutting if(!limit && dp[pos][axis][sum]!=-1) return dp[pos][axis][sum]; //has been calculate int up=limit ? a[pos] : 9; __int64 ans=0; for(int i=0;i<=up;i++) { ans+=dfs(pos-1,sum+i*(pos-axis),axis,limit && i==a[pos]); //using sum of negative } //end of calculation if(!limit) dp[pos][axis][sum]=ans; return ans;}__int64 solve(__int64 x) //x = maximum{ int pos=0; if(x<0) { return 0; } while(x) { a[++pos]=x%10; x/=10; } //start form digit 1 __int64 ans = 0; int len = pos; for(int i=1;i<=len;i++) { ans+=dfs(pos,0,i,1); } return ans-len+1;}int main(){ __int64 li,ri; int T_T; cin>>T_T; memset(dp,-1,sizeof(dp)); while(T_T--) { scanf("%I64d%I64d",&li,&ri); printf("%I64d\n",solve(ri)-solve(li-1)); } return 0;}
阅读全文
0 0
- HDOJ 3709 balanced number (digit dp)
- hdoj 3709 Balanced Number 【数位dp】
- HDOJ 3709: Balanced Number
- HDOJ 3709 Balanced Number
- HDOJ 3790 Balanced Number
- HDU 3709 Balanced Number(数位DP)
- 数位dp HDU 3709 Balanced Number
- hdu 3709 Balanced Number (数位dp)
- hdu 3709 Balanced Number(数位dp)
- Balanced Number - HDU 3709 数位dp
- [数位dp] hdu 3709 Balanced Number
- 【HDU】3709 Balanced Number 数位DP
- HDU 3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number(数位DP)
- 【数位DP】 HDU 3709 Balanced Number
- hdu 3709Balanced Number(数位dp)
- HDU 3709 Balanced Number (简单数位DP)
- HDU 3709 Balanced Number (数位DP)
- 锁的种类与特点
- 支付功能集成包-简单设计
- hive 认证
- 剑指offer--二进制中的1的个数--补码
- Android Studio导入Eclipse项目操作步骤 在选SDK时一定不要使用默认,要改成19,否则运行按键就是灰色
- HDOJ 3709 balanced number (digit dp)
- PHP源码安装后找不到配置文件的问题处理
- 获取HUD的方法
- poj dfs相关之1321 棋盘问题
- String,StringBuffer和StringBuilder的区别
- Android 解决apk覆盖安装的时候,出现安装失败,与旧版本部兼容的问题
- Oracle DB Time 解读
- ubuntu下gearman安装
- c++二叉排排序树的实现 补上了删除和查找操作