这题可能非递归版好写?
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,a,b,dp[12][5000],ret=0,bit[12]; void get_table() { for (int i=0;i<=9;i++) dp[1][i]=1; for (int i=0;i<=4599;i++) dp[0][i]=1; for (int i=2;i<=8;i++) for (int j=0;j<=4599;j++) for (int k=0;k<=9;k++) if (j>=k*(1<<(i-1))) dp[i][j]+=dp[i-1][j-k*(1<<(i-1))]; for (int i=1;i<=8;i++) for (int j=1;j<=4599;j++) dp[i][j]+=dp[i][j-1]; } void get_bit(int x) { ret=0; while (x) {bit[++ret]=x%10;x/=10;} } int f(int x) { get_bit(x);int ans=0; for (int i=ret;i>=1;i--) ans=(ans<<1)+bit[i]; return ans; } int dfs(int pos,int ret) { if (!pos) return 1; int i,ans=0; for (i=0;i<bit[pos];i++) { if (ret>=i*(1<<(pos-1))) ans+=dp[pos-1][ret-i*(1<<(pos-1))]; else break; } if (i!=bit[pos]) return ans; if (ret>=bit[pos]*(1<<(pos-1))) ans+=dfs(pos-1,ret-bit[pos]*(1<<(pos-1))); return ans; } void work(int x) { int ff=f(a);get_bit(b); printf("Case #%d: %d ",x,dfs(ret,ff)); } int main() { scanf("%d",&t);get_table(); for (int i=1;i<=t;i++) { scanf("%d%d",&a,&b); work(i); } return 0; }