URAL 1057. Amount of Degrees(数位DP)

题目链接

我看错题了。。。都是泪啊,不存在3*4^2这种情况。。。系数必须为1。。。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <vector>
 5 using namespace std;
 6 #define LL long long
 7 int num[60];
 8 LL dp[60][60];
 9 int k,b;
10 LL dfs(int pos,int pre,int bound)
11 {
12     int i,end;
13     LL ans = 0;
14     if(pos == -1)
15     return pre == 0;
16     if(pre < 0)
17     return 0;
18     if(!bound&&dp[pos][pre] != -1)
19     return dp[pos][pre];
20     end = bound ? num[pos]:b-1;
21     for(i = 0;i <= end;i ++)
22     {
23         if(i == 0)
24         ans += dfs(pos-1,pre,bound&&i == end);
25         else if(i == 1)
26         ans += dfs(pos-1,pre-1,bound&&i == end);
27     }
28     if(!bound)
29     dp[pos][pre] = ans;
30     return ans;
31 }
32 LL judge(LL x)
33 {
34     int pos = 0;
35     while(x)
36     {
37         num[pos++] = x%b;
38         x /= b;
39     }
40     return dfs(pos-1,k,1);
41 }
42 int main()
43 {
44     LL x,y;
45     memset(dp,-1,sizeof(dp));
46     cin>>x>>y>>k>>b;
47     cout<<judge(y)-judge(x-1)<<endl;
48     return 0;
49 }

 

posted @ 2013-10-31 18:21  Naix_x  阅读(186)  评论(0)    收藏  举报