~~~~2013成都网赛f(x) hdu4734 数位dp~~~~

本文介绍了一个数值权重计算问题,即对于一个十进制数x,定义其权重F(x),并探讨了如何计算在0到B范围内,权重不超过F(A)的数的数量。文章通过预处理方法,使用动态规划技巧来高效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F(x)

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



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
 

Recommend
liuyiding


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
#define N 100010
#define eps (1e-8)
#define INF 99999999
using namespace std;
int c[10][9*512];//c[i][j]表示有i位且f(x)<=j的x个数;
void init()
{
    int i,j,k;
    for(i=0;i<10;i++)
        c[i][0]=1;//开始忘记了初始化这个了,WA了好久。。。哎
    for(i=0;i<9*512;i++)
        c[0][i]=1;
    for(i=0;i<9*512;i++)
        c[1][i]=min(10,i+1);
    for(i=2;i<=9;i++)
        for(j=1;j<9*512;j++)
        {
            for(k=0;k<=9 && j-k*(1<<(i-1))>=0;k++)
                    c[i][j]+=c[i-1][j-k*(1<<(i-1))];
        }
}
int f(int x)
{
    int ans=0,q=1;
    while(x)
    {
        ans+=q*(x%10);
        q*=2;
        x/=10;
    }
    return ans;
}
int solve(int a,int b)
{
    int s[11],d=0,ans=0;
    if(a==0) return 1;
    while(b)
    {
        s[d++]=b%10;
        b/=10;
    }
    for(d--;d>=0;d--)
    {
        for(int i=0;i<s[d];i++)
        {
            if(a-i*(1<<d)<0) return ans;
            ans+=c[d][a-i*(1<<d)];
        }
        a-=s[d]*(1<<d);
        if(a<0) return ans;
    }
    return ans;
}
int main()
{
    int t,cas=1;
    init();
    scanf("%d",&t);
    while(t--)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        printf("Case #%d: %d\n",cas++,solve(f(a),b+1));
    }
    return 0;
}

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.Language
92149842013-09-23 15:27:17Accepted473431MS492K1567 BG++


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值