hdu 4734 F(x)(数位DP,4级)
F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 443 Accepted Submission(s): 160
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
Recommend
liuyiding
数位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)); } } }
The article write by nealgavin