您的位置:首页 > 其它

HDU_4734 F(x)[数位dp]

2014-12-01 12:30 441 查看
                                                                                                                  
F(x)

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 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 < 10 9)
 

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

题意:求0 - B之间 F(x)<=F(A)的x的个数。

思路:数位dp,递归模板。500Ms的卡时间。dp[ i ] [ j ],i位数,函数值不大于 j的个数。

#include<iostream>
#include<cstring>
#include<cstdio>
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long LL;

int dp[11][100010];
int t_2[11];
int a,len;
char b[11];

int dfs(int pos,int s,int flag) // flag表示是否是边界值,1表示是,0表示不是
{
if(pos==0) return s>=0;
if(s<0) return 0;
if(!flag&&dp[pos][s]!=-1) return dp[pos][s];
int num=flag?b[len-pos]-'0':9;
int ans=0;
for(int i=0;i<=num;i++)
ans+=dfs(pos-1,s-i*t_2[pos-1],flag&&i==num);
if(!flag) return dp[pos][s]=ans;
return ans;

}

int main()
{
for(int i=0;i<=10;i++)
t_2[i]=(1<<i);
int T;
scanf("%d",&T);
mem(dp,-1);
for(int cas=1;cas<=T;cas++)
{

scanf("%d%s",&a,b);
int f_a=0;
len=strlen(b);
int x=1;
while(a)
{
f_a+=(a%10)*x;
x<<=1;
a/=10;
}

printf("Case #%d: %d\n",cas,dfs(len,f_a,1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: