F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A).
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
For each test case, there are two numbers A and B (0 <= A,B < 109)
For each test case, there are two numbers A and B (0 <= A,B < 109)
Output
For every case,you should output "Case #t: " at first, without quotes. The t is the case number starting from 1. Then output the answer.
Sample Input
3
0 100
1 10
5 100
Sample Output
Case #1: 1
Case #2: 2
Case #3: 13
Source
数位dp;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-4 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; ll f[20][N],bit[20]; ll dp(int pos,int pre,int flag) { if(pre<0)return 0; if(pos==0)return (pre>=0); if(flag&&f[pos][pre]!=-1)return f[pos][pre]; ll x=flag?9:bit[pos]; ll ans=0; for(ll i=0;i<=x;i++) { ans+=dp(pos-1,pre-i*(1<<(pos-1)),flag||i<x); } if(flag)f[pos][pre]=ans; return ans; } int check(ll x) { int sum=0,base=1; for(int i=0;x;i++,base*=2,x/=10) sum+=(x%10)*base; return sum; } ll getans(ll x,ll y) { int len=0; while(x) { bit[++len]=x%10; x/=10; } return dp(len,check(y),0); } int main() { int T,cas=1; scanf("%d",&T); memset(f,-1,sizeof(f)); while(T--) { ll l,r; scanf("%lld%lld",&l,&r); //cout<<check(l)<<endl; printf("Case #%d: %lld ",cas++,getans(r,l)); } return 0; }