数位DP。。。。

F(x)

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

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
30 100
1 10
5 100
 

Sample Output
Case #1: 1
Case #2: 2
Case #3: 13
 

Source
 

Recommend
liuyiding
 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long int LL;

LL dp[12][111111];
int bit[12];

int dfs(int pos,int sum,bool limit)
{
    if(pos==-1) return 1;
    if(~dp[pos][sum]&&limit==false) return dp[pos][sum];
    int end=limit?bit[pos]:9;
    int res=0;
    for(int i=0;i<=end;i++)
    {
        if((sum-i*(1<<pos))>=0)
            res+=dfs(pos-1,sum-i*(1<<pos),limit&&i==end);
    }
    if(!limit)
        dp[pos][sum]=res;
    return res;
}

int getsum(int x)
{
    int l=1,sum=0;
    while(x)
    {
        sum+=l*(x%10);
        x/=10; l=l*2;
    }
    return sum;
}

LL colu(int x,int y)
{
    int pos=0,sum=getsum(y);
    while(x)
    {
        bit[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,sum,true);
}

int main()
{
    int cas=1,x,y,t;
    memset(dp,-1,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&y,&x);
        printf("Case #%d: %I64d\n",cas++,colu(x,y));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 4734 F(x)的更多相关文章

  1. 【数位DP】 HDU 4734 F(x)

    原题直通车:HDU 4734 F(x) 题意:F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1, 求0.....B中F[x]<=F[A ...

  2. HDU 4734 F(x) 2013 ACM/ICPC 成都网络赛

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4734 数位DP. 用dp[i][j][k] 表示第i位用j时f(x)=k的时候的个数,然后需要预处理下小 ...

  3. HDU 4734 - F(x) - [数位DP][memset优化]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 Time Limit: 1000/500 MS (Java/Others) Memory Lim ...

  4. 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 ...

  5. HDU 4734 F(x)

    这题可能非递归版好写? #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  6. hdoj 2802 F(N)【递推 规律】

    F(N) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. HDOJ 2802 F(N)

    Problem Description Giving the N, can you tell me the answer of F(N)? Input Each test case contains ...

  8. 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  ...

  9. 题解——HDU 4734 F(x) (数位DP)

    这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...

随机推荐

  1. POJ 2484 A Funny Game(博弈论)

    题目链接: 传送门 A Funny Game Time Limit: 1000MS     Memory Limit: 10000K Description Alice and Bob decide ...

  2. PHP ServerPush (推送) 技术的探讨

    2016年11月29日17:51:03 转自:http://www.cnblogs.com/hnrainll/archive/2013/05/07/3064874.html 需求: 我想做个会员站内通 ...

  3. Linux Basis --- commands of vi

    EDIT mode to GENERAL mode: press ESC general mode: CLOSE FILE :q!    :force to close the file but no ...

  4. ionic中获取坐标方法

    ionic中获取坐标的方法 1.首相需要执行命令: cordova plugin add cordova-plugin-geolocation2.然后块级注入配置bower文件引入ngCordova ...

  5. JavaScript中的变量及数据类型

    转自:http://blog.csdn.net/mygis2005/article/details/7375419 JavaScript是一种弱类型的语言,变量名.操作符和方法名都区分大小写. 1.变 ...

  6. NSNotificationCenter(通知)与Key-Value Coding (KVC)与Key-Value Observing (KVO)

    NSNotificationCenter //下面是不同类中需要实现的代码 1.[[NSNotificationCenter defaultCenter] addObserver:self selec ...

  7. 【原】CSS3 Media在常用设备的设置值

    摘要:今天的一个小小的项目中,在各种手机上样式都显示正常,唯独iphone4s的有些许问题.产品经理又说iphone4s用户还挺多的,无奈,只能查一查iphone4s的media值,顺便做个小小总结; ...

  8. sql lock

    要提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑.其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善在线大量查询 ...

  9. bootstrap从phpcmsv9中数据库中取出照片达到自适应的效果

    <script type="text/javascript"> $(".col-xs-12 img").addClass("carouse ...

  10. AspectJ基础学习之二搭建环境(转载)

    AspectJ基础学习之二搭建环境(转载) 一.下载Aspectj以及AJDT 上一章已经列出了他的官方网站,自己上去download吧.AJDT是一个eclipse插件,开发aspectj必装,他可 ...