Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <bitset> #include <map> #include <queue> #include <stack> #include <vector> #define rep(i,m,n) for(i=m;i<=n;i++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define sys system("pause") const int maxn=1e5+10; const int N=5e4+10; const int M=N*10*10; using namespace std; inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline void umax(ll &p,ll q){if(p<q)p=q;} inline void umin(ll &p,ll q){if(p>q)p=q;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int n,m,k,t,num[20],pos,cas; ll dp[10][5000]; ll dfs(int pos,int x,int y) { if(pos<0)return x>=0; if(x<0)return 0; if(y&&dp[pos][x]!=-1)return dp[pos][x]; int now=y?9:num[pos],i; ll ret=0; rep(i,0,now) { ret+=dfs(pos-1,x-qpow(2,pos)*i,y||i<num[pos]); } return y?dp[pos][x]=ret:ret; } ll gao(int p) { pos=0; while(p)num[pos++]=p%10,p/=10; return dfs(pos-1,n,0); } int main() { int i,j; memset(dp,-1,sizeof(dp)); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); k=j=0; while(n)j+=qpow(2,k++)*(n%10),n/=10; n=j; printf("Case #%d: %lld\n",++cas,gao(m)); } return 0; }
原文:http://www.cnblogs.com/dyzll/p/6390259.html