0
点赞
收藏
分享

微信扫一扫

HDU 4734-F(x)(数位DP)


F(x)

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

 

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

 

 

Source

​​2013 ACM/ICPC Asia Regional Chengdu Online ​​

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  ​​6437​​​ ​​6436​​​ ​​6435​​​ ​​6434​​​ ​​6433​​ 

 不过我觉得有点错误,下面有的地方是我的理解

题意:(好好看)

给了个f(x)的定义:F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1,Ai是十进制数位,然后给出a,b求区间[0,b]内满足f(i)<=f(a)的i的个数。

分析:

常规想:这个f(x)计算就和数位计算是一样的,就是加了权值,所以dp[pos][sum],这状态是基本的。a是题目给定的,f(a)是变化的不过f(a)最大好像是4600的样子。如果要memset优化就要加一维存f(a)的不同取值,那就是dp[10][4600][4600],这显然不合法。

这个时候就要用减法了dp[pos][sum]表示pos位数比sum小的数的个数,也就是说初始的是时候sum是f(a),枚举一位就减去这一位在计算f(i)的权值,那么最后枚举完所有位 sum>=0时就是满足的,后面的位数凑足sum位就可以了。

仔细想想这个状态是与f(a)无关的(新手似乎很难理解),一个状态只有在sum>=0时满足

代码实现:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#define lson id<<1,l,m
#define rson id<<1|1,m+1,r

using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=1ll<<60;
const double PI=acos(-1.0);
int n,m;
int A;
int B;
int dp[11][20000];
int num[11];
int f(int x)
{
int len=0;
int sum=0;
for (len=0; x; len++)
{
sum+=(x%10)*(1<<len);
x/=10;
}
return sum;
}
int dfs(int pos,int sum,bool f)
{

if (pos==-1) return sum>=0;
if (sum<0) return 0;
if (!f && dp[pos][sum]!=-1) return dp[pos][sum];
int u;
if (f) u=num[pos];
else u=9;
int ans=0;
for (int i=0; i<=u; i++)
{
ans+=dfs(pos-1,sum-i*(1<<pos),f&&(i==u));
}
if (!f) dp[pos][sum]=ans;
return ans;
}
int calc(int x)
{
int len=0;
for (len=0; x; len++)
num[len]=x%10,x/=10;
int up=f(A);
return dfs(len-1,up,true);
}
int main()
{
int tt;
scanf("%d",&tt);
int cnt=0;
memset(dp,-1,sizeof dp);
while(tt--)
{
scanf("%d%d",&A,&B);
int ans=calc(B);
printf("Case #%d: %d\n",++cnt,ans);
}
return 0;
}

 

举报

相关推荐

0 条评论