一般的数位DP的题目都是在问我们从【l,r】满足条件的数字的个数 ——————我们按位来加快枚举
#include
using namespace std;
const int N=20;
int dp[N][N];
int a[N],len;
int n,m;
//pos 代表当前位置 limit是当前位置是否有这个枚举范围限制
int dfs(int pos,int pre,int limit){
if(!pos)return 1;
if(!limit&&~dp[pos][pre])return dp[pos][pre];
int res = 0,up = limit?a[pos]:9;
for(int i=0;i<=up;i++){
if(i==4||(i==2&&pre==6))continue;
res += dfs(pos-1,i,limit&&i==up);
}
return limit?res:dp[pos][pre] = res;
}
int cal(int x){
memset(dp,-1,sizeof dp);
len = 0;
while(x)a[++len] = x%10,x/=10;
return dfs(len,0,1);
}
int main()
{
while(cin>>n>>m,n){
cout<
#include
using namespace std;
const int N=20;
int dp[N][N];
int a[N],len;
int dfs(int pos,int pre,int lead,int limit){
if(!pos)return 1;
if(!lead&&!limit&&~dp[pos][pre])return dp[pos][pre];
int res = 0,up = limit?a[pos]:9;
for(int i=0;i<=up;i++){
if(abs(i-pre)<2)continue;
if(lead&&!i)res+=dfs(pos-1,-2,lead&&!i,limit&&i==up);
else res+=dfs(pos-1,i,lead&&!i,limit&&i==up);
}
return lead?res:(limit?res:dp[pos][pre] = res);
}
int cal(int x){
memset(dp,-1,sizeof dp);
len = 0;
while(x)a[++len] = x%10,x/=10;
return dfs(len,-2,1,1);
}
int n,m;
int main()
{
cin>>n>>m;
cout<
#include
using namespace std;
const int N=35;
int dp[N][N];
int a[N],len;
int n,m,k,b;
int dfs(int pos,int cnt,int limit){
if(!pos)return cnt==k;
if(!limit&&~dp[pos][cnt])return dp[pos][cnt];
int res = 0,up = limit?a[pos]:b-1;
for(int i=0;i<=up;i++){
if(i>1||(i==1&&cnt==k))continue;
res+=dfs(pos-1,cnt+(i==1),limit&&i==up);
}
return limit?res:dp[pos][cnt]=res;
}
int cal(int x){
memset(dp,-1,sizeof dp);
len = 0;
while(x)a[++len] = x%b,x/=b;
return dfs(len,0,1);
}
int main()
{
cin>>n>>m>>k>>b;
cout<
#include
using namespace std;
const int N=40;
int dp[N][10100];
int a[N],len;
int n,m,k;
int dfs(int pos,int sum,int limit){
if(!pos)return sum%k==0;
if(!limit&&~dp[pos][sum])return dp[pos][sum];
int res = 0, up = limit?a[pos]:9;
for(int i=0;i<=up;i++)
res+=dfs(pos-1,sum+i,limit&&i==up);
return limit?res:dp[pos][sum] = res;
}
int cal(int x){
memset(dp,-1,sizeof dp);
len = 0;
while(x)a[++len] = x%10,x/=10;
return dfs(len,0,1);
}
int main()
{
while(cin>>n>>m>>k){
cout<