数位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. Integrated Circuit Intro

    1.Noise Margin 不管是TTL还是CMOS Logic Level,都会有门限.如,5V CMOS的VoutH > 4.4V, VoutL <0.33V;而它对应的输入电平Vi ...

  2. python统计nginx脚本信息

    #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib2 import json import subprocess import th ...

  3. 关于当一个C#工程移植到另一台机子上(win7)上时,程序报错。dll没有被指定在Windows上运行,或者它包含错误。请尝试使用原始安装媒体重新安装程序。。。。。。

    , 解决方法:通过从网上重新下载dll文件 拷贝到报错的目录下,替换掉原有的dll,可以正确运行.

  4. Linux下pdf阅读器推荐

    由于需要在pdf文件上做标记,所以自带的文档查看器根本满足了需求,之前去网上查了查,Okular评价挺高,就安装了一个,确实能基本满足我的需求,但是 1.界面感觉还是不太友好,书签栏一直在那. 2.而 ...

  5. [JavaEE] Entity中Lazy Load的属性序列化JSON时报错

    The server encountered an internal error that prevented it from fulfilling this request.org.springfr ...

  6. soapUI使用-DataSource获取oracle库中的参数

    soapUI使用-DataSource获取oracle库中的参数 下载mysql和oracle驱动包:http://pan.baidu.com/s/1i3sy1MH 放在Program Files\S ...

  7. BZOJ2186: [Sdoi2008]沙拉公主的困惑

    传送门 常规数论题,利用欧拉函数的相关性质. 题求$[1,N!]$中与$M!$互质的数的个数,且$M \leq N$.然后根据欧拉函数的相关性质很容易得出这道题的答案为$\frac{\phi (M!) ...

  8. python wmi使用

    python wmi 官方开发文档https://msdn.microsoft.com/en-us/library/aa394388(v=vs.85).aspx WMI使用的WIN32_类库名 htt ...

  9. FFT质数打表程序

    #include<bits/stdc++.h> using namespace std; typedef long long ll; void sol(ll x){ int y=0; fo ...

  10. wpf arcglobe +c# 三维缩放到图层

    /// <summary>        /// 地图缩放到图层        /// </summary>        /// <param name="s ...