H - F(x)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <string>
 6 using namespace std;
 7 typedef long long ll;
 8 
 9 const int maxn = 1e4+5;
10 int dp[15][maxn];    //pos sum
11 int Max;
12 int aa[15];
13 
14 int f(int x){
15     int ans = 0;
16     int cnt = 1;
17     while(x){
18         ans = ans + x%10*cnt;
19         x /= 10;
20         cnt *= 2;
21     }
22     return ans;
23 }
24 
25 int dfs(int pos, int sum, bool limit){
26     if(pos == -1) return sum <= Max;    //是否满足条件
27     if(sum > Max) return 0;
28     if(!limit && dp[pos][Max - sum] != -1)
29         return dp[pos][Max - sum];
30     int up = limit?aa[pos]:9;
31     int ans = 0;
32     for(int i = 0; i <= up;i++){
33         ans += dfs(pos-1, sum+ i*(1<<pos), limit&&i == aa[pos]);
34     }
35     if(!limit) dp[pos][Max - sum] = ans;
36     return ans;
37 }
38 
39 int solve(int x){
40     int pos = 0;
41     while(x){
42         aa[pos++] = x%10;
43         x /= 10;
44     }
45     return dfs(pos-1, 0, true);    //最高位是有限制的
46 }    
47 
48 int main(){
49     memset(dp, -1, sizeof dp);
50     int t;
51     scanf("%d", &t);
52     int Case = 0;
53     while(t--){
54         // memset(dp, -1, sizeof(dp));
55         Case++;
56         int a, b;
57         scanf("%d%d", &a, &b);
58         Max = f(a);
59         int ans = solve(b);
60         printf("Case #%d: %d\n", Case, ans);
61     }
62     return 0;
63 }

 

H - F(x)

posted @ 2018-05-30 19:37  ouyang_wsgwz  阅读(406)  评论(0编辑  收藏  举报