Hdu 4734 F(X) 数位dp

F(x)

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


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
 
 
题意:定义F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A1 * 1。 输入两个数A、B,求0~b中有多少数x,使得F(x) <= F(A);
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long

int dig[30];
ll dp[30][10000];
ll Fa;

ll dfs(int pos, ll maxx, int lim) {
    if(pos == -1) return maxx >= 0;
    if(maxx < 0) return 0;
    if(!lim && dp[pos][maxx] != -1) return dp[pos][maxx];
    int End = lim ? dig[pos] : 9;
    ll res = 0;
    for(int i = 0; i <= End; i++) {
        res += dfs(pos - 1, maxx - i * (1 << pos), lim && (i == End));
    }
    if(!lim) dp[pos][maxx] = res;
    return res;
}

ll func(ll a, ll num) {
    int n = 0;
    if(a == 0) return 1;
    while(num) {
        dig[n++] = num % 10;
        num /= 10;
    }
    int base = 1;
    while(a) {
        Fa += (a % 10) * base;
        a /= 10; base *= 2;
    }
    dfs(n - 1, Fa, 1); 
}

int main() {
    int t;
    scanf("%d", &t);
    memset(dp, -1, sizeof(dp));
    ll a, b;
    int val = 0;
    while(t--) {
        Fa = 0;
        scanf("%I64d %I64d", &a, &b);
        printf("Case #%d: %I64d\n", ++val, func(a, b));
    }
}

 

posted @ 2016-07-09 21:14  MartinEden  阅读(116)  评论(0编辑  收藏  举报