HDU 4734 —— F(x)

F(x)

Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

For a decimal number x with n digits (A nn-1n-2 ... A 21), we define its weight as

F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A 1 * 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 < 10 9)

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

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int b[10], num[10];
int dp[10][4605];

int dfs(int u, int f, bool limit)
{
    if(u < 1)    return 1;
    if(!limit && dp[u][f] != -1)    return dp[u][f];

    int maxn = limit ? b[u] : 9;
    int ret = 0, cur;

    for(int i=0; i<=maxn; i++) {
        cur = f - i*(1<<u-1);
        if(cur > 0) {
            ret += dfs(u-1, cur, limit && i==maxn);
        } else if(cur == 0) {
            ret += 1;
        }
    }

    if(!limit)    dp[u][f] = ret;
    return ret;
}

int f(int n, int a)
{
    int lenb=0;
    memset(b, 0, sizeof(b));
    while(n) {
        b[++lenb] = n%10;
        n /= 10;
    }
    return dfs(lenb, a, 1);
}

int main ()
{
    num[0] = 1;
    for(int i=1; i<10; i++) {
        num[i] = num[i-1] * 10;
    }

    int T, A, B;
    scanf("%d", &T);
    memset(dp, -1, sizeof(dp));
    for(int kase=1; kase<=T; kase++) {
        scanf("%d%d", &A, &B);
        int base = 1, F=0;
        while(A) {
            F += base * (A % 10);

            base <<= 1;
            A /= 10;
        }
        printf("Case #%d: %d\n", kase, f(B, F));
    }
    return 0;
}
时间: 2024-10-11 06:42:24

HDU 4734 —— F(x)的相关文章

HDU 4734 F(x)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:对于一个n位的十进制数字x=(AnAn-1An-2 ... A2A1),定义 F(x)=An*2n-1+An-1*2n-2+ ...+A2*2+A1*1.给出A.B,求在[0,B]之间有多少数字满足F(x)<=F(A)? 思路:数位DP.f[dep][x]表示到达dep剩余为x的方案数. i64 n,m; i64 f[25][N]; int a[25],num; i64 Sum; i64

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

HDU 4734——F(x)

HDU 4734——F(x) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:数i在0~B的范围内,求F(i)小于F(A)的个数 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1 先把F(A)算出来=tot,然后按位计算,如果num(当前F(pos)<=tot,方案数+1 返回1,如果num>tot,返回0) dp[pos][num] 的状态表示为dp[当前第几位][最大值t

hdu 4734 F(x)(数位dp+优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~b有多少个不大于f(a)的数 显然这题可以设这样的dp dp[len][count]表示前len位权值为count的有多少,然后显然的在len==0时return count>=f(a); 但是这样

HDU 4734 F(x)(数位DP)

Description For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there betwe

HDU 4734 F(x) (数位DP,基础)

题意:  一个非负整数的十进制位是这样的 (AnAn-1An-2 ... A2A1),定义F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1.给出A和B,问[0,B]中有几个整数x的F(x)值<=F(A)? 思路: 算一下就知道F(x)值不会超过512*9,而B仅仅有8位十进制数,那么8*512*9就可以算出所有的统计了.对于每个询问,先计算F(A)的值,然后统计小于此值有几个就行了.统计的复杂度也是很低的.若是以前缀和来统计后面的个数的

HDU - 4734 F(x) (2013成都网络游戏,数字DP)

意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

HDU 4734 F(x) (数位DP)

F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 382    Accepted Submission(s): 137 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x

HDU - 4734 F(x) (数位DP)

For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A 1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B,