【HDU4734】F(x) 数位Dp

5人阅读 评论(0) 收藏 举报
分类:

F(x)

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

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

题解

数位DP学习笔记
对于这一个题,我们先计算出Af(x)值,然后状态记录枚举到当前位的f(x)值,最后返回的时候要判断是否小于Af(x),不需要判前导0;

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
const int MAXN=1e4+5;
using namespace std;
int A,B,n;
int dp[12][MAXN];
int Fina;
int a[12],Num;
int clac(int x)
{
    if(x==0) return 0;
    int ans=clac(x/10);
    return ans*2+(x%10);
}
int dfs(int pos,int sum,bool limit)
{
    if(pos==-1) return sum<=Fina;
    if(sum>Fina) return 0;
    if(dp[pos][Fina-sum]!=-1&&(!limit)) return dp[pos][Fina-sum];
    int up=limit ? a[pos] : 9;
    int Tmp=0;
    for(int i=0;i<=up;i++)
    {
        Tmp+=dfs(pos-1,sum+i*(1<<pos),limit && i==a[pos]);
    }
    if(!limit) dp[pos][Fina-sum]=Tmp;
    return Tmp;
}
int solve(int x)
{
    Fina=clac(A);
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);
}
int main()
{
    scanf("%d",&n);
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&A,&B);
        printf("Case #%d: %d\n",++Num,solve(B));
    }
    return 0;
}
查看评论

hdu4734——F(x) (数位DP)

F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis...
  • qq_20200047
  • qq_20200047
  • 2017-04-14 21:11:45
  • 67

hdu4734,F(x), 数位dp

裸的数位dp,是我写的第二道数位dp。 #include #include #include #include #define INF 200000000 int dp[11][5050],a,b[...
  • u014287583
  • u014287583
  • 2014-03-27 13:14:04
  • 296

hdu4734 F(x) 数位dp

没写过几个数位dp的题,拿来练练手..dp[i][j]表示在没有高位限制的情况下,第i位...
  • yanglei040
  • yanglei040
  • 2014-09-04 11:24:44
  • 559

hdu4734 F(x)(数位dp)

按时吃
  • Flynn_curry
  • Flynn_curry
  • 2016-04-24 21:23:30
  • 166

HDU4734 F(x)(数位DP)

F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm...
  • Rain722
  • Rain722
  • 2017-08-05 11:37:30
  • 110

HDU4734——F(x)(数位DP)

dp[i][j]表示i位数权值不超过j的数的个数 注意点: dp[i][j]的值不用每次都初始化,因为它的值不受输入的影响,如果前面算过了就直接拿来用,没算过就拿来算并记录下来 #inc...
  • u014141559
  • u014141559
  • 2014-07-23 21:22:43
  • 689

hdu4734 F(x) (数位dp)

Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its wei...
  • Kirito_Acmer
  • Kirito_Acmer
  • 2016-03-20 11:17:55
  • 275

hdu4734 F(x) 数位dp

//对于一个十进制的数A,其每一位的权值为2^(n-1)得到的值即为 //这个数的权值,现在给出A,B //问在区间[0,B]中的所有数权值小于等于 //A的权值的个数为多少 //dp[i][j] 表...
  • cq_pf
  • cq_pf
  • 2015-09-23 22:31:21
  • 247

HDU4734 F(x)[数位DP]

D - F(x) HDU - 4734 题意:求出存在[0,B]中x使得F(x)小于等于F(A)的数有多少个。题解:首先求出F(A)的值,用这个值作为标记的初始值,第一维代表当前位置,第二维代表剩余值...
  • ControlBear
  • ControlBear
  • 2017-07-01 10:17:20
  • 193

【HDU4734】F(x) 数位DP

题目描述   For a decimal number x with n digits (A[n]A[n-1]A[n-2] … A[2]A[1]), we define its weight as...
  • qq_25554407
  • qq_25554407
  • 2017-03-31 08:47:25
  • 202
    个人资料
    持之以恒
    等级:
    访问量: 1939
    积分: 792
    排名: 6万+
    友链