码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4734 F(x) (数位dp)

时间:2015-04-28 01:50:36      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

F(x)

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


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
3 0 100 1 10 5 100
 

 

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

 

Source
 

 

Recommend
liuyiding
 

 

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<cstring>
 6 #define ll long long
 7 
 8 using namespace std;
 9 
10 int n;
11 int p[12];
12 int a,b;
13 int fa,fb;
14 int num[12];
15 int dp[12][25010];
16 
17 void init() {
18     p[0]=1;
19     for(int i=1; i<=10; i++)
20         p[i]=p[i-1]*2;
21 }
22 
23 int dfs(int i,int s,bool e) {
24     if(i==-1)return s>=0;
25     if(s<0)return 0;
26     if(!e&&dp[i][s]!=-1)return dp[i][s];
27     int u=e?num[i]:9;
28     int res=0;
29     for(int d=0; d<=u; d++) {
30         int news=s-d*p[i];
31         res+=dfs(i-1,news,e&&d==u);
32     }
33     return e?res:dp[i][s]=res;
34 }
35 
36 int solve(int x) {
37     int len=0;
38     while(x) {
39         num[len++]=x%10;
40         x/=10;
41     }
42     return dfs(len-1,fa,1);
43 }
44 
45 int main() {
46     //freopen("in.txt","r",stdin);
47     memset(dp,-1,sizeof dp);
48     init();
49     int t;
50     int ca=1;
51     cin>>t;
52     while(t--) {
53         scanf("%d%d",&a,&b);
54         int k=0;
55         fa=0;
56         while(a) {
57             fa+=p[k]*(a%10);
58             a/=10,k++;
59         }
60         //cout<<"fa="<<fa<<endl;
61         printf("Case #%d: %d\n",ca++,solve(b));
62     }
63     return 0;
64 }

 

hdu 4734 F(x) (数位dp)

标签:

原文地址:http://www.cnblogs.com/White-Bread/p/4461833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!