HDU 4734 F[x] (数位dp)

来源:互联网 发布:java 游戏引擎 编辑:程序博客网 时间:2024/06/07 06:12

#include <stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[20];int dp[20][6000];int p[50];int all;int dfs(int pos,int sta,bool limit){if(pos==-1)return 1;if(!limit&&dp[pos][sta]!=-1){return dp[pos][sta]; }int up=limit?a[pos]:9;int sum=0,i;for(i=0;i<=up;i++){int k=sta-i*p[pos];if(k<0) continue;sum+=dfs(pos-1,k,limit&&i==up);}if(!limit)dp[pos][sta]=sum;return sum;}int solve(int x){int cnt=0;while(x){a[cnt++]=x%10;x/=10;}return dfs(cnt-1,all,1);}int main(int argc, char *argv[]){int t,i,cs=1;scanf("%d",&t);memset(dp,-1,sizeof(dp));p[0]=1;for(i=1;i<30;i++){p[i]=p[i-1]*2;}while(t--){int A,B;while(scanf("%d %d",&A,&B)!=EOF){all=0;int cnt1=0;while(A){all+=(A%10)*p[cnt1++];A/=10;}printf("Case #%d: ",cs++);printf("%d\n",solve(B));}}return 0;}

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6851    Accepted Submission(s): 2649


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)
 

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
30 1001 105 100
 

Sample Output
Case #1: 1Case #2: 2Case #3: 13
 

Source
原创粉丝点击