F(x)
HDU - 4734For each test case, there are two numbers A and B (0 <= A,B < 10 9)
3 0 100 1 10 5 100
Case #1: 1 Case #2: 2 Case #3: 13
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e4+5;
int dp[12][N];
int f(int x){
if(x == 0)return 0;
int ans = f(x/10);
return ans*2+(x%10);
}
int all;
int a[12];
int dfs(int pos,int sum,int limit){
if(pos == -1) return sum<=all;
if(sum > all) return 0;
if(!limit && dp[pos][all-sum]!=-1) return dp[pos][all-sum];
int up = limit ? a[pos] : 9;
int ans = 0;
int i;
for(i = 0; i <= up; i++){
ans += dfs(pos-1,sum+i*(1<<pos),limit&&i==a[pos]);
}
if(!limit) dp[pos][all-sum] = ans;
return ans;
}
int solve(int x){
int pos = 0;
while(x){
a[pos++] = x%10;
x /= 10;
}
return dfs(pos-1,0,true);
}
int main(){
int a,b;
int t;
int cas = 0;
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
while(t--){
scanf("%d%d",&a,&b);
all = f(a);
printf("Case #%d: %d\n",++cas,solve(b));
}
return 0;
}
4381



被折叠的 条评论
为什么被折叠?



