hdu4734——F(x) (数位DP)

时间:2022-12-16 10:46:22

F(x)

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


Problem 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
 

Source

2013 ACM/ICPC Asia Regional Chengdu Online 

题意:找出i在0到b之间的f(i)小于等于f(a)的数的个数。

思路:数位dp。主要在于状态转移不好想。dp[i][j]表示i位数比j小的数的个数。用递归完成的话就只需要思考边界和状态转移。

边界:

dp[i][j]如果j小于0,显然是dp[i][j]=0的,如果i==0,说明就是0,显然任何数都比0大,所以dp[i][j]对于j>=0的时候dp[i][j]=1,否则dp[i][j]=0

状态转移:

dp[i][j]+=dp[i-1][j-k*(1<<(i-1))];

完成上述两步推导就能开始写这题了。




#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0x3f3f3f3f
#define mod 100000007


const int M=1005;
int n,m;
int cnt;
/*int sx,sy,sz;
int mp[1000][1000];
int pa[M*10],rankk[M];
int head[M*6],vis[M*100];
int dis[M*100];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
//vector<int> g[M*10];


int has[10500];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};


void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
struct node
{
    int v,w;
    node(int vv,int ww)
    {
        v=vv;
        w=ww;
    }
};
vector<int> g[M*100];
string str[1000];
*/
int dp[20][200000];
int bit[50];
int dfs(int cur,int s,int e,int z){
    if(s<0)return 0;//不能光相信模板,这么实际的情况都米想到
    if(cur<0) return s>=0;//s的初值是F(n),递归到头s>=0就是当前数小于F(n)return check(s);
    if(!e&&!z&&dp[cur][s]!=-1) return dp[cur][s];
    int endx=e?bit[cur]:9;
    int ans=0;
    for(int i=0;i<=endx;i++){
            //if(i==4||s&&i==2)continue;
        if(z&&!i) ans+=dfs(cur-1,s-i*(1<<cur),e&&i==endx,1);
        else ans+=dfs(cur-1,s-i*(1<<cur),e&&i==endx,0);
            //ans+=dfs(cur-1,get_news(s,i),e&&i==endx,0);
    }
    if(!e&&!z) dp[cur][s]=ans;
    return ans;
}
int F(int a){
    int ans=0,len=0;
    while(a){
        ans+=(a%10)*(1<<len);
        len++;
        a/=10;
    }
    return ans;
}
int solve(int x){
    int len=0;
    while(x){
        bit[len++]=x%10;
        x/=10;
    }
    return dfs(len-1,F(n),1,1);
}
int main()
{
    int i,j,k,t;
    int l,r,cas=0;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));//对于数位DP,memset拿到外边就行,因为统计的数位信息会有被重用的可能
    while(t--)
    {
        scanf("%d%d",&n,&m);
        printf("Case #%d: %d\n",++cas,solve(m));
    }
    return 0;
}