Hdu 4734 F(x) (数位dp)

题目链接

F(x)

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


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

题解:F(x)的最大值不超过5000,可以用数位dp来做。

用dp[i][j]表示0到i位还没有填(即不超过i位的所有数字中),要让F值小于等于等于j的方案数。

dp[i][j]+=dp[i-1][j-x*(1<<i)], 0<=x<=9;

数位统计问题的具体统计方法可以参加相关数位dp的论文。

代码如下:

#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int nn = 3100;
const int inf = 0x3fffffff;
int A,B;
int wei[20];
int dp[20][5000];
int F(int x)
{
    int re=0;
    int ix=0;
    while(x)
    {
        if(x%10)
            re+=x%10*(1<<ix);
        x/=10;
        ix++;
    }
    return re;
}
int dfs(int id,int lim,bool man)
{
    if(lim<0)
        return 0;
    if(id==-1)
    {
        return 1;
    }
    if(!man&&dp[id][lim]!=-1)
        return dp[id][lim];
    int re=0;
    int en=man?wei[id]:9;
    for(int i=0;i<=en;i++)
    {
        re+=dfs(id-1,lim-i*(1<<(id)),man&&(i==en));
    }
    if(!man)
        dp[id][lim]=re;
    return re;
}
int solve()
{
    int ix=0;
    memset(wei,0,sizeof(wei));
    while(B)
    {
        if(B%10)
        {
            wei[ix]=B%10;
        }
        ix++;
        B/=10;
    }
    return dfs(10,F(A),true);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&A,&B);
        printf("Case #%d: %d\n",cas++,solve());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值