hdu_3709_Balanced Number(数位DP)
2016-05-29 17:23
295 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3709
题意:给你一个区间,让你找平衡数的个数
题解:设dp[i][j][k]为前i位以第j位为支撑点的力矩和为k的方案数,注意的是0,00,000这些也是平衡数,所以要减掉一个len长度
#include<cstdio> #include<cstring> #define F(i,a,b) for(LL i=a;i<=b;i++) typedef long long LL; LL t,n,m,dp[20][20][2010],dig[20],len,an,an2,ii; LL dfs(int pos,int now,int f,bool inf){ if(!pos)return !f; if(f<0)return 0; if(!inf&&dp[pos][now][f]!=-1)return dp[pos][now][f]; LL end=inf?dig[pos]:9,ans=0; F(i,0,end)ans+=dfs(pos-1,now,f+(pos-now)*i,inf&&(i==end)); if(!inf)dp[pos][now][f]=ans; return ans; } int main(){ memset(dp,-1,sizeof(dp)); scanf("%lld",&t); while(t--){ scanf("%lld%lld",&n,&m),n--; for(len=0;n;n/=10)dig[++len]=n%10; for(an=0,ii=len;ii>0;ii--)an+=dfs(len,ii,0,1);an-=len; for(len=0;m;m/=10)dig[++len]=m%10; for(an2=0,ii=len;ii>0;ii--)an2+=dfs(len,ii,0,1); printf("%lld\n",an2-len-an); } return 0; }View Code
相关文章推荐
- alert() 与 console.log() 的区别
- 二叉树的简单递归实现(创建,遍历,高度,大小)
- 二叉树的非递归遍历
- Linux中常用的查询指令(which、whereis、find、locatae)
- 如何将你的vim配置的“高大上”
- ctags 和Makefile的简单使用
- 学习进度第十三周
- Unity性能优化(CPU)
- TOJ 4146 4147 4148 4149 4150 4151 4152 4153 4154
- 2016百度之星复赛 1003 拍照 扫描线
- 编写一个函数reverse_string(char * string)(递归实现)
- 二叉树的简单操作
- 模拟实现常用字符串函数
- memcpy函数和memmove函数的模拟实现与区别
- 回调函数实现冒泡法的多种排序
- C语言常见单链表面试题(1)
- C语言常见单链表面试题(2)
- C语言注释转换为c++的注释
- c++中string类的基本功能的实现(1)
- c/c++单链表面试题—链表带环问题