题意把一个int数二进制拆分
然后给你一个a叫你输出f(x)<=f(a)的所有值
我们咋做呢 看了别人的题解 用一个二维dp dp[pos][sum]代表到pos位置凑出sum需要多少值
我们要注意 sum一定是<=all的 所以如果sum>all 那么直接return 0
然后由于不受前导0影响 所以直接用一个limit就行了
由于本质不会被查询改变 所以我们在多组输入外面用一次memset就可以了
/*
20181013
hdu 4734
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
int a[20];
ll dp[20][10005];
int all;
int f(int x){
int ans = 0,pos = 0;
while(x){
ans += ((x%10)<<pos);
pos++;
x/=10;
}
return ans;
}
ll dfs(int pos,int sum,bool limit){
if(pos==-1) return sum<=all;
if(sum>all) return 0;
if(!limit&&dp[pos][all-sum]!=-1) return dp[pos][all-sum];
ll tmp = 0;
int up = limit?a[pos]:9;
for(int i = 0;i<=up;++i){
tmp+=dfs(pos-1,sum+i*(1<<pos),limit&&i==a[pos]);
}
if(!limit) dp[pos][all-sum] = tmp;
return tmp;
}
ll solve(int x){
int pos = 0;
while(x){
a[pos++] = x%10;
x/=10;
}
return dfs(pos-1,0,true);
}
int main(){
int t;
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
for(int Case = 1;Case<=t;++Case){
int a,b;
scanf("%d%d",&a,&b);
all = f(a);
printf("Case #%d: %lld\n",Case,solve(b));
}
return 0;
}