数位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. (转载)LCA问题的Tarjan算法

    转载自:Click Here LCA问题(Lowest Common Ancestors,最近公共祖先问题),是指给定一棵有根树T,给出若干个查询LCA(u, v)(通常查询数量较大),每次求树T中两 ...

  2. POJ 1365 Prime Land(数论)

    题目链接: 传送门 Prime Land Time Limit: 1000MS     Memory Limit: 10000K Description Everybody in the Prime ...

  3. Linux以外的开源操作系统大汇总

    开源操作系统即公开源代码的操作系统软件,它遵循开源协议使用.编译和发布.自由和开放源代码软件中最著名的是Linux,它是一种类Unix的操作系统.Linux可安装在各种计算机硬件设备中,比如手机.平板 ...

  4. Ubuntu学习总结-04 搭建JAVA开发环境

    JAVA开发环境是一种跨平台的程序设计语言,可以在windows.LINUX等操作系统上进行开发. 1 下载JDK 从以下地址下所需的jdk安装包 . http://www.oracle.com/te ...

  5. iOS - CABasicAnimation

    代码实例: [1] - (void)pulseClick { //!> 宽和高等比例转换 CABasicAnimation * pulse = [CABasicAnimation animati ...

  6. C++ Virtual

    摘自:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html namespace QCAR { /// Area is the ...

  7. 捉襟见肘之NSMutableSet和NSPointerArray

    用来学习复习记录,其他优秀的译文,点击这里 一.NSMutableSet NSMutableSet和NSMutableArray存放数据方式分别是无序和有序,这说明,数组是可以通过index获取对象. ...

  8. 理解和使用 JavaScript 中的回调函数

    理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报  分类: JavaScript(4)    目录( ...

  9. SaltStack之Job管理和Runner(八)

    SaltStack之Job管理和Runner 配置文件/etc/salt/master cachedir: /var/cache/salt/master # cache路径 keep_jobs: 24 ...

  10. Yocto开发笔记之《网卡配置》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ============================================== # ifconfig -a # ...