HDUOJ-----F(x)
F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 468 Accepted Submission(s): 171
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
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include <string.h> 3 int sty[10][5000]; 4 int pow1[10]={1,2,4,8,16,32,64,128,256,512}; 5 int pow2[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; 6 7 int cal(int ca,int b); 8 int f(int a); 9 int main() 10 { 11 int i,j,a,b,l,number,I,count; 12 memset(sty,0xff,5000*10*sizeof(int)); 13 while(scanf("%d",&number)!=EOF) 14 { 15 for(I=1;I<=number;I++) 16 { 17 scanf("%d%d",&a,&b); 18 a=f(a); 19 b++; 20 count=0; 21 for(i=9;i>=0;i--) 22 { 23 l=b/pow2[i]; 24 b-=l*pow2[i]; 25 for(j=0;j<l;j++) 26 count+=cal(i,a-j * pow1[i]); 27 a-=l*pow1[i]; 28 } 29 printf("Case #%d: %d\n",I,count); 30 } 31 } 32 return 0; 33 } 34 35 int cal(int ca,int b) 36 { 37 if(b<0) 38 return 0; 39 if(ca==0) 40 return 1; 41 if(sty[ca][b]!=-1) 42 return sty[ca][b]; 43 int n = 0,i; 44 for(i = 0;i < 10;i++) 45 n += cal(ca - 1,b - i * pow1[ca-1]); 46 sty[ca][b] = n; 47 return n; 48 } 49 int f(int a) 50 { 51 int n=0,l,i; 52 for(i=9;i>=0;i--) 53 { 54 l=a/pow2[i]; 55 n+=l*pow1[i]; 56 a-=l*pow2[i]; 57 } 58 return n; 59 }
看了别人的思路,写的
编程是一种快乐,享受代码带给我的乐趣!!!