3 0 100 1 10 5 100
Case #1: 1 Case #2: 2 Case #3: 13
数位DP:长度为x,值小于y的最优值可以满足所有,因此不用重复初始化
#include<iostream> #include<cstring> #include<cstdio> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) #define ll(x) (1<<x) using namespace std; int dp[13][9*ll(11)];///长度为x,<y的个数值 int bit[13],A,B,low[13]; int F(int x) { int ret=0,z=1; while(x) { ret+=x%10*z;z<<=1;x/=10; } return ret; } int DP(int pp,int st,bool big) { if(pp==0)return st>=0; if(big&&dp[pp][st]!=-1)return dp[pp][st]; int kn=big?9:bit[pp]; int ret=0; FOR(i,0,kn) { ret+=DP(pp-1,st-i*low[pp-1],big||kn!=i); } if(big)dp[pp][st]=ret; return ret; } int get(int a,int b) { int pos=0; while(b) { bit[++pos]=b%10;b/=10; } return DP(pos,F(a),0); } int main() { low[0]=1; FOR(i,1,10)low[i]=low[i-1]*2; int cas;clr(dp,-1); while(~scanf("%d",&cas)) { FOR(ca,1,cas) { scanf("%d%d",&A,&B); printf("Case #%d: ",ca); printf("%d\n",get(A,B)); } } }