HDU 4734 F(x) (2013成都网络赛,数位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 (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, 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  

 

Source
2013 ACM/ICPC Asia Regional Chengdu Online  

 

Recommend liuyiding  

 

 

 

数位DP的水题

dp[i][j]表示i位值<=j 的总数

 

/* ***********************************************
Author        :kuangbin
Created Time  :2013/9/14 星期六 12:45:42
File Name     :2013成都网络赛\1007.cpp
************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int dp[20][200000];

int bit[20];



int dfs(int pos,int num,bool flag)
{
    if(pos == -1)return num >= 0;
    if(num < 0)return 0;
    if(!flag && dp[pos][num] != -1)
        return dp[pos][num];
    int ans = 0;
    int end = flag?bit[pos]:9;
    for(int i = 0;i <= end;i++)
    {

        ans += dfs(pos-1,num - i*(1<<pos),flag && i==end);
    }
    if(!flag)dp[pos][num] = ans;
    return ans;
}

int F(int x)
{
    int ret = 0;
    int len = 0;
    while(x)
    {
        ret += (x%10)*(1<<len);
        len++;
        x /= 10;
    }
    return ret;
}
int A,B;
int calc()
{
    int len = 0;
    while(B)
    {
        bit[len++] = B%10;
        B/=10;
        //cout<<bit[len-1]<<endl;
    }
    //cout<<F(A)<<endl;
    return dfs(len-1,F(A),1);
}




int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int iCase = 0;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--)
    {
        iCase++;
        scanf("%d%d",&A,&B);
        printf("Case #%d: %d\n",iCase,calc());
    }
    return 0;
}

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3321997.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞