hdu4734 F(x)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn=1e5+5;
 6 int tot,e[15];
 7 int c[15][maxn];
 8 int t,a,b,cas;
 9 template<class t>void red(t &x)
10 {
11     int w=1;
12     x=0;
13     char ch=getchar();
14     while(ch>'9'||ch<'0')
15     {
16         if(ch=='-')
17             w=-1;
18         ch=getchar();
19     }
20     while(ch>='0'&&ch<='9')
21     {
22         x=(x<<3)+(x<<1)+ch-'0';
23         ch=getchar();
24     }
25     x*=w;
26 }
27 void input()
28 {
29     freopen("input.txt","r",stdin);
30     //freopen("output.txt","w",stdout);
31 }
32 void dv(int x)
33 {
34     tot=0;
35     while(x)
36     {
37         e[++tot]=x%10;
38         x/=10;
39     }
40     e[tot+1]=0;
41 }
42 int dfs(int pos,bool limit,int sum)
43 {
44     if(!pos)
45         return sum>=0;
46     if(sum<0)
47         return 0;
48     if(!limit&&c[pos][sum]!=-1)
49         return c[pos][sum];
50     int up=limit?e[pos]:9;
51     int ans=0;
52     for(int i=0;i<=up;++i)
53         ans+=dfs(pos-1,limit&&(i==up),sum-i*(1<<(pos-1)));
54     if(!limit)
55         c[pos][sum]=ans;
56     return ans;
57 }
58 int f(int x)
59 {
60     int ans=0,i=0;
61     while(x)
62     {
63         ans+=(1<<(i))*(x%10);
64         x/=10;
65         ++i;
66     }
67     return ans;
68 }
69 int solve()
70 {
71     //memset(c,-1,sizeof(c));
72     dv(b);
73     return dfs(tot,1,f(a));
74 }
75 int main()
76 {
77     //input();
78     red(t);
79     memset(c,-1,sizeof(c));
80     while(t--)
81     {
82         red(a);
83         red(b);
84         printf("Case #%d: %d\n",++cas,solve());
85     }
86     return 0;
87 }
View Code

 

posted @ 2019-06-10 20:57  Achen_sy  阅读(155)  评论(0编辑  收藏  举报