标签:
3 0 100 1 10 5 100
Case #1: 1 Case #2: 2 Case #3: 13
1 #include <bits/stdc++.h> 2 using namespace std; 3 int dp[10][200010],pos[10]; 4 int F(int x) { 5 int b = 1,ret = 0; 6 while(x) { 7 ret += x%10*b; 8 x /= 10; 9 b <<= 1; 10 } 11 return ret; 12 } 13 int dfs(int len,int now,bool flag) { 14 if(len < 0) return now >= 0; 15 if(now < 0) return 0; 16 if(!flag && dp[len][now] != -1) return dp[len][now]; 17 int d = flag?pos[len]:9,ret = 0; 18 for(int i = 0; i <= d; ++i) 19 ret += dfs(len-1,now-i*(1<<len),flag&&i==d); 20 if(!flag) dp[len][now] = ret; 21 return ret; 22 } 23 int calc(int a,int b) { 24 int cnt = 0; 25 while(b) { 26 pos[cnt++] = b%10; 27 b /= 10; 28 } 29 return dfs(cnt-1,F(a),true); 30 } 31 int main() { 32 int kase,a,b; 33 scanf("%d",&kase); 34 memset(dp,-1,sizeof dp); 35 for(int cs = 1; cs <= kase; ++cs) { 36 scanf("%d%d",&a,&b); 37 printf("Case #%d: %d\n",cs,calc(a,b)); 38 } 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4712607.html