前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ-----F(x)

HDUOJ-----F(x)

作者头像
Gxjun
发布2018-03-21 13:09:17
5410
发布2018-03-21 13:09:17
举报
文章被收录于专栏:ml

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

2013 ACM/ICPC Asia Regional Chengdu Online

Recommend

liuyiding

代码:

代码语言:javascript
复制
 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 }

看了别人的思路,写的

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • F(x)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档