[ZJOI2010] digital count explanations

Face questions

This question is a digital template of the DP title;

Because the nest too konjac, so not recursive, only the memory of the search;

First of all, be ye ye violence;

The decomposed into a number of an array, so that later facilitate the call;

Digital techniques :( with the DP answer 1 ~ b) - (1 ~ a answer) is (a ~ b answer);

Then for each digital i, we do twice dfs (upper bound respectively to a to b and upper bound);

Based digital searching in digit:

Every enumeration, when the == digit, put the answer +1, and the memory of;

Then gone;

But to do so overlooks two important things:

1. The leading zeros may be present;

2. The current search to a larger number than the target value;

For these two things, recording limit = 1 represents the upper bound instead of this one is the upper bound of the target in dfs, otherwise this is not a requirement;

head = 1 represents the current number of search to the absence of a leading zero;

It is clear that the pseudo-code :()

long long dfs(int pos,int limit,int lead,int digit,long long sum)

the Register int up = 9 ; // this one up represents the upper bound of 

IF (limit) up = NUM [POS]; inc(j,0,up) ans+=dfs(pos-1,(j==up)&&limit,lead||j,digit,sum+((j||lead)&&(j==digit)));

Make good use of bit operation, then pay attention to the memory of, then you can the AC;

#include <bits/stdc++.h>
#define inc(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
long long a,b,f[34][3400],num[34];
long long  dfs(int pos,int limit,int lead,int digit,long long sum)
{
    long long ans=0;
    if(pos<=0) return sum;      	
    if(!limit&&lead&&f[pos][sum]!=-1) return f[pos][sum];
    register int up=9; if(limit) up=num[pos];  
    inc(j,0,up) ans+=dfs(pos-1,(j==up)&&limit,lead||j,digit,sum+((j||lead)&&(j==digit)));  	
    if(!limit&&lead) f[pos][sum]=ans;
    return years;
}
long long work(long long x,register int type)
{
    memset(f,-1,sizeof(f));
    register int len=0;
    while(x){
        num [len ++] = x 10%;
        x/=10;
    }
    return dfs(len,1,0,type,0);
}
int main ()
{
    cin>>a>>b;
    for(register int i=0;i<=9;i++){
        cout<<work(b,i)-work(a-1,i);
        if(i!=9) cout<<" ";
    }
}
/*
1 99
*/

 

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11794726.html