hdu 4734

F(x)

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

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

3 0 100 1 10 5 100

Sample Output

Case #1: 1 Case #2: 2 Case #3: 13

数位dp,用dp【x】【y】代表取到第x位小于等于F(Y)的个数,dfs多开一维记录F(Y),逐减看F(Y)的值

学习了大神博客:http://www.cnblogs.com/kuangbin/p/3321997.htm

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
int dp[10][10240];
int bit[10];
int dfs(int len,int num,bool flag) //flag为1代表前位已选定,答案完整
{                                  //否则答案还不完整
    if(len==-1) return num>=0;
    if(num<0) return 0;
    if(!flag&&dp[len][num]!=-1) return dp[len][num];
    int end=flag?bit[len]:9;
    int ans=0;
    for(int i=0;i<=end;i++)
    {
        ans+=dfs(len-1,num-(i*(pow(2,len))),flag&&i==end);
    }
    if(!flag) dp[len][num]=ans;
    return ans;
}
int F(int A)
{
    int x=1;
    int ans=0;
    while(A)
    {
        ans+=(A%10)*x;
        x<<=1;
        A/=10;
    }
    return ans;
}
int cacu(int x,int A)
{
    int top=0;
    while(x)
    {
      bit[top++]=x%10;
      x/=10;
    }
    return dfs(top-1,F(A),1);
}
int main()
{
   int T,ca=1;
   memset(dp,-1,sizeof(dp));
   for(scanf("%d",&T);T;T--)
   {
       int A,B;
       scanf("%d%d",&A,&B);
       printf("Case #%d: %d\n",ca++,cacu(B,A));
   }
   return 0;
}

时间: 2024-11-16 08:12:27

hdu 4734的相关文章

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 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, plea

HDU 4734 F(x)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:对于一个n位的十进制数字x=(AnAn-1An-2 ... A2A1),定义 F(x)=An*2n-1+An-1*2n-2+ ...+A2*2+A1*1.给出A.B,求在[0,B]之间有多少数字满足F(x)<=F(A)? 思路:数位DP.f[dep][x]表示到达dep剩余为x的方案数. i64 n,m; i64 f[25][N]; int a[25],num; i64 Sum; i64

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

HDU 4734——F(x)

HDU 4734——F(x) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:数i在0~B的范围内,求F(i)小于F(A)的个数 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1 先把F(A)算出来=tot,然后按位计算,如果num(当前F(pos)<=tot,方案数+1 返回1,如果num>tot,返回0) dp[pos][num] 的状态表示为dp[当前第几位][最大值t

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)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~b有多少个不大于f(a)的数 显然这题可以设这样的dp dp[len][count]表示前len位权值为count的有多少,然后显然的在len==0时return count>=f(a); 但是这样

HDU 4734 F(x)(数位DP)

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 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there betwe

HDU 4734 —— F(x)

F(x) Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 yo

HDU 4734 F(x) (数位DP,基础)

题意:  一个非负整数的十进制位是这样的 (AnAn-1An-2 ... A2A1),定义F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1.给出A和B,问[0,B]中有几个整数x的F(x)值<=F(A)? 思路: 算一下就知道F(x)值不会超过512*9,而B仅仅有8位十进制数,那么8*512*9就可以算出所有的统计了.对于每个询问,先计算F(A)的值,然后统计小于此值有几个就行了.统计的复杂度也是很低的.若是以前缀和来统计后面的个数的

HDU - 4734 F(x) (2013成都网络游戏,数字DP)

意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -