Hdu 3709 Balanced Number

来源:互联网 发布:php linux 删除文件夹 编辑:程序博客网 时间:2024/05/21 05:21

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3709

思路:平衡数。枚举平衡位置,记忆化搜索的方式记录已有的值。加剪枝,排除掉重复的0。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <map>#include <queue>#include <algorithm>using namespace std;#define LL long long#define Maxn 20LL dp[Maxn][Maxn][2005];int digit[Maxn];LL dfs(int pos,int pivot,int pre,bool limit){   if(pos<=0) return pre == 0;   if(pre<0) return 0;   if(!limit && dp[pos][pivot][pre]!=-1) return dp[pos][pivot][pre];   int end = limit ? digit[pos] : 9;   LL ans = 0;   for(int i=0;i<=end;i++)   {      ans += dfs(pos-1,pivot,pre + i*(pos-pivot),limit && (i == end));   }   if(!limit) dp[pos][pivot][pre] = ans;   return ans;}LL calc(LL a){   if(a<0) return 0;   int len = 0;   LL ans = 0;   while(a>0)   {      digit[++len] = a%10;      a/=10;   }   for(int i=1;i<=len;i++)   {      ans += dfs(len,i,0,1);   }   ans = ans - len + 1;   return ans;}int main(){   int t;   LL x,y;   scanf(" %d",&t);   memset(dp,-1,sizeof(dp));   while(t--)   {      scanf(" %I64d %I64d",&x,&y);      printf("%I64d\n",calc(y) - calc(x-1) );   }   return 0;}
0 0
原创粉丝点击