【HDOJ】【3709】Balanced Bumber

数位DP


题解:http://www.cnblogs.com/algorithms/archive/2012/09/02/2667637.html

 

dfs的地方没太看懂……(也就那里是重点吧喂!)挖个坑……回头再看看

 1 //HDOJ 3709
 2 #include<cmath>
 3 #include<vector>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<iostream>
 8 #include<algorithm>
 9 #define rep(i,n) for(int i=0;i<n;++i)
10 #define F(i,j,n) for(int i=j;i<=n;++i)
11 #define D(i,j,n) for(int i=j;i>=n;--i)
12 #define pb push_back
13 using namespace std;
14 int getint(){
15     int v=0,sign=1; char ch=getchar();
16     while(ch<'0'||ch>'9'){ if (ch=='-') sign=-1; ch=getchar();}
17     while(ch>='0'&&ch<='9'){ v=v*10+ch-'0'; ch=getchar();}
18     return v*=sign;
19 }
20 const int N=20,M=1400;
21 typedef long long LL;
22 /******************tamplate*********************/
23 
24 LL dp[N][N][M];
25 int bit[N];
26 LL dfs(int pos,int o,int sum,int f){
27     if (sum<0) return 0;
28     if (pos==0) return sum==0;
29     if (!f && dp[pos][o][sum]!=-1) return dp[pos][o][sum];
30 
31     LL ret=0;
32     int max=f ? bit[pos] : 9;
33     F(i,0,max)
34         ret+=dfs(pos-1,o,sum+(pos-o)*i,f && i==max);
35     if (!f) dp[pos][o][sum]=ret;
36     return ret;
37 }
38 
39 LL cal(LL n){
40     int len=0;
41     for(;n;n/=10) bit[++len]=n%10;
42     LL ret=0;
43     F(o,1,len)
44         ret+=dfs(len,o,0,1);
45     return ret-len+1;
46 }
47 int main(){
48 #ifndef ONLINE_JUDGE
49     freopen("3709.in","r",stdin);
50     freopen("3709.out","w",stdout);
51 #endif
52     memset(dp,-1,sizeof dp);
53     int T=getint();
54     while(T--){
55         LL x,y;
56         cin >> x >> y;
57         cout << cal(y)-cal(x-1)<<endl;
58     }
59     return 0;
60 }
View Code

 

posted @ 2015-03-01 22:36  Tunix  阅读(209)  评论(0编辑  收藏  举报