F(x)

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn=1e4+5;
 8 
 9 int A,B,sum;
10 int num[40],dp[40][maxn];
11 
12 int F(int x){
13     if(x==0) return 0;
14     int ans=F(x/10);
15     return ans*2+(x%10);
16 } 
17 
18 int DFS(int pos,int res,bool F){
19     if(pos==-1) return res<=sum;
20     if(res>sum) return 0;
21     if(!F&&dp[pos][sum-res]!=-1) return dp[pos][sum-res];
22     
23     int maxv=F?num[pos]:9;
24     int ans=0;
25     for(int i=0;i<=maxv;i++) ans=ans+DFS(pos-1,res+i*(1<<pos),F&&i==maxv);
26     
27     if(!F) dp[pos][sum-res]=ans;
28     return ans;
29 }
30 
31 int Solve(int temp){
32     if(temp==0) return 1;
33     int cnt=0;
34     while(temp){
35         num[cnt++]=temp%10;
36         temp/=10;
37     }
38     return DFS(cnt-1,0,true);
39 }
40 
41 int main()
42 {   int kase;
43     cin>>kase;
44     memset(dp,-1,sizeof(dp));            //放在循坏里面会超时
45     for(int t=1;t<=kase;t++){
46         cin>>A>>B;
47         sum=F(A);
48         printf("Case #%d: %d\n",t,Solve(B));
49     }
50     return 0;
51 }

 

 1 int DFS(int pos,int res,bool F){
 2     if(res>sum) return 0;           
 3     if(pos==-1) return 1;
 4     if(!F&&dp[pos][sum-res]!=-1) return dp[pos][sum-res];
 5     
 6     int maxv=F?num[pos]:9;
 7     int ans=0;
 8     for(int i=0;i<=maxv;i++) ans=ans+DFS(pos-1,res+i*(1<<pos),F&&i==maxv);
 9     
10     if(!F) dp[pos][sum-res]=ans;
11     return ans;
12 }

 

posted @ 2017-09-21 11:38  天之道,利而不害  阅读(987)  评论(0编辑  收藏  举报