算法笔记--数位dp

算法笔记

这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392

数位dp的精髓是不同情况下sta变量的设置。

模板:

int a[20];  
ll dp[20][state];//不同题目状态不同  
ll dfs(int pos,/*state变量*/,bool lead/*前导零*/,bool limit/*数位上界变量*/)//不是每个题都要判断前导零  
{  
    //递归边界,既然是按位枚举,最低位是0,那么pos==-1说明这个数我枚举完了  
    if(pos==-1) return 1;/*这里一般返回1,表示你枚举的这个数是合法的,那么这里就需要你在枚举时必须每一位都要满足题目条件,也就是说当前枚举到pos位,一定要保证前面已经枚举的数位是合法的。不过具体题目不同或者写法不同的话不一定要返回1 */  
    //第二个就是记忆化(在此前可能不同题目还能有一些剪枝)  
    if(!limit && !lead && dp[pos][state]!=-1) return dp[pos][state];  
    /*常规写法都是在没有限制的条件记忆化,这里与下面记录状态是对应,具体为什么是有条件的记忆化后面会讲*/  
    int up=limit?a[pos]:9;//根据limit判断枚举的上界up;这个的例子前面用213讲过了  
    ll ans=0;  
    //开始计数  
    for(int i=0;i<=up;i++)//枚举,然后把不同情况的个数加到ans就可以了  
    {  
        if() ...  
        else if()...  
        ans+=dfs(pos-1,/*状态转移*/,lead && i==0,limit && i==a[pos]) //最后两个变量传参都是这样写的  
        /*这里还算比较灵活,不过做几个题就觉得这里也是套路了 
        大概就是说,我当前数位枚举的数是i,然后根据题目的约束条件分类讨论 
        去计算不同情况下的个数,还有要根据state变量来保证i的合法性,比如题目 
        要求数位上不能有62连续出现,那么就是state就是要保存前一位pre,然后分类, 
        前一位如果是6那么这意味就不能是2,这里一定要保存枚举的这个数是合法*/  
    }  
    //计算完,记录状态  
    if(!limit && !lead) dp[pos][state]=ans;  
    /*这里对应上面的记忆化,在一定条件下时记录,保证一致性,当然如果约束条件不需要考虑lead,这里就是lead就完全不用考虑了*/  
    return ans;  
}  
ll solve(ll x)  
{  
    int pos=0;  
    while(x)//把数位都分解出来  
    {  
        a[pos++]=x%10;//个人老是喜欢编号为[0,pos),看不惯的就按自己习惯来,反正注意数位边界就行  
        x/=10;  
    }  
    return dfs(pos-1/*从最高位开始枚举*/,/*一系列状态 */,true,true);//刚开始最高位都是有限制并且有前导零的,显然比最高位还要高的一位视为0嘛  
}  
int main()  
{  
    ll le,ri;  
    while(~scanf("%lld%lld",&le,&ri))  
    {  
        //初始化dp数组为-1,这里还有更加优美的优化,后面讲  
        printf("%lld
",solve(ri)-solve(le-1));  
    }  
}  

例题1:HDU 2089 不要62

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset((a),(b),sizeof(a))
int a[20];
int dp[20][2];

int dfs(int pos,int pre,int sta,bool limit)
{
    if(pos==-1)return 1;
    if(!limit&&dp[pos][sta]!=-1) return dp[pos][sta];
    int up=limit?a[pos]:9;
    int t=0;
    for(int i=0;i<=up;i++)
    {
        if(pre==6&&i==2)continue;
        if(i==4)continue;
        t+=dfs(pos-1,i,i==6,limit&&i==a[pos]);
    }
    if(!limit)dp[pos][sta]=t;
    return t;
}

int solve(int n)
{
    int pos=0;
    while(n)
    {
        a[pos++]=n%10;
        n/=10;
    }
    return dfs(pos-1,-1,0,true);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int l,r;
    while(cin>>l>>r&&(l||r))
    {
        mem(dp,-1);
        cout<<solve(r)-solve(l-1)<<endl;
    }    
    return 0;
} 
View Code

例题2:POJ 3252 Round Numbers

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
#define ll long long
#define pb push_back 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair 
#define pii pair<int,int>
#define pi acos(-1.0)

const int INF=0x3f3f3f3f;
int a[50];
int dp[50][66];

int dfs(int pos,int sta,bool lead,bool limit)
{
    if(pos==-1)return sta>=32;
    if(!lead&& !limit&&dp[pos][sta]!=-1)return dp[pos][sta];
    int up=limit?a[pos]:1;
    int ans=0;
    for(int i=0;i<=up;i++)
    {
        if(lead&&i==0)ans+=dfs(pos-1,sta,true,limit&&i==a[pos]);//有前导零不算进sta里 
        else ans+=dfs(pos-1,sta+(i==0?1:-1),false,limit&&i==a[pos]);
    }
    if(!lead&& !limit)dp[pos][sta]=ans;
    return ans;
}

int solve(int n)
{
    int c=0;
    while(n)
    {
        a[c++]=n&1;
        n>>=1;
    }
    return dfs(c-1,32,true,true);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a,b;
    mem(dp,-1);
    cin>>a>>b;
    cout<<solve(b)-solve(a-1)<<endl;
    return 0;
}
View Code

例题3:HDU 3555 Bomb

方法1:和例题1差不多,先求出不含49的个数,然后再用n+1减去这个个数就是答案

代码1:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
#define ll long long
#define pb push_back 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair 
#define pii pair<int,int>
#define pi acos(-1.0)

const int INF=0x3f3f3f3f;
ll dp[20][2];
int a[20];

ll dfs(int pos,int pre,int sta,bool limit)
{
    if(pos==-1)return 1;
    if(!limit&&dp[pos][sta]!=-1)return dp[pos][sta];
    ll ans=0;
    int up=limit?a[pos]:9;
    for(int i=0;i<=up;i++)
    {
        if(pre==4&&i==9)continue;
        ans+=dfs(pos-1,i,i==4,limit&&i==a[pos]);
    }
    if(!limit)dp[pos][sta]=ans;
    return ans;
}
ll solve(ll n)
{
    int c=0;
    while(n)
    {
        a[c++]=n%10;
        n/=10;
    }
    return dfs(c-1,0,0,true);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    ll n;
    cin>>t;
    mem(dp,-1);
    while(t--)
    {
        cin>>n;
        cout<<n-solve(n)+1<<endl;
    }
    return 0;
}
View Code

方法2:直接求含49的个数

代码2:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
#define ll long long
#define pb push_back 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair 
#define pii pair<int,int>
#define pi acos(-1.0)
int a[20];
ll dp[20][3];


ll dfs(int pos,int sta,bool limit)//sta:0:前1位不是4且前面没出现过49;1:前1位是4;2:前面出现过49 
{
    if(pos==-1) return sta==2;
    if(!limit&&dp[pos][sta]!=-1)return dp[pos][sta];
    ll ans=0;
    int up=limit?a[pos]:9;
    for(int i=0;i<=up;i++)
    {
        int tsta;
        if(sta==0)
        {
            if(i==4)tsta=1;
            else tsta=0;
        }
        else if(sta==1)
        {
            if(i==4)tsta=1;
            else if(i==9)tsta=2;
            else tsta=0;
        }
        else tsta=2;
        ans+=dfs(pos-1,tsta,limit&&i==a[pos]);
    } 
    if(!limit)dp[pos][sta]=ans;
    return ans;
}

ll solve(ll n)
{
    int c=0;
    while(n)
    {
        a[c++]=n%10;
        n/=10;
    }
    return dfs(c-1,0,true);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    ll n;
    mem(dp,-1);
    cin>>t;
    while(t--)
    {
        cin>>n;
        cout<<solve(n)<<endl;
    }
    return 0; 
} 
View Code

例题4:HDU 3652 B-number

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
#define ll long long
#define pb push_back 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair 
#define pii pair<int,int>
#define pi acos(-1.0)

const int INF=0x3f3f3f3f;
int dp[15][15][3];
int a[15];

int dfs(int pos,int mod,int sta,bool limit)//sta表示:0:前一位不为 1且前面没有出现过13;1:前一位为1;2:前面出现过13。 
{
    if(pos==-1) return (mod==0)&&(sta==2);
    if(!limit&&dp[pos][mod][sta]!=-1)return dp[pos][mod][sta];
    int ans=0;
    int up=limit?a[pos]:9;
    for(int i=0;i<=up;i++)
    {
        int tmod=(mod*10+i)%13;
        int tsta;
        if(sta==2)tsta=2;
        else if(sta==1)
        {
            if(i==1)tsta=1;
            else if(i==3)tsta=2;
            else tsta=0;
        }
        else if(sta==0)
        {
            if(i==1)tsta=1;
            else tsta=0;
        }
        ans+=dfs(pos-1,tmod,tsta,limit&&i==a[pos]);
    }
    if(!limit)dp[pos][mod][sta]=ans;
    return ans;
}

int solve(int n)
{
    int c=0;
    while(n)
    {
        a[c++]=n%10;
        n/=10;
    }
    return dfs(c-1,0,0,true);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    mem(dp,-1);
    while(cin>>n)
    {
        cout<<solve(n)<<endl;
    }    
    return 0;
}
View Code

例题5:Codeforces 55D - Beautiful numbers

数位dp+数论+离散化

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm> 
using namespace std;
#define ll long long
#define pb push_back 
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair 
#define pii pair<int,int>
#define pi acos(-1.0)
ll dp[20][50][2521];
int a[20];
int m[2521];

int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}

int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}

ll dfs(int pos,int num,int prelcm,bool limit)
{
    if(pos==-1) return num%prelcm==0;
    if(!limit&&dp[pos][m[prelcm]][num]!=-1) return dp[pos][m[prelcm]][num];
    ll ans=0;
    int up=limit?a[pos]:9;
    for(int i=0;i<=up;i++)
    {
        ans+=dfs(pos-1,(num*10+i)%2520,i?lcm(prelcm,i):prelcm,limit&&i==a[pos]);
    }
    if(!limit) dp[pos][m[prelcm]][num]=ans;
    return ans;
}

ll solve(ll n)
{
    int c=0;
    while(n)
    {
        a[c++]=n%10;
        n/=10;
    }
    return dfs(c-1,0,1,true);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    ll l,r;
    int c=1;//要从1开始啊,因为m[0]=0 
    for(int i=1;i<=2520;i++)
    {
        if(2520%i==0)m[i]=c++; 
    }
    mem(dp,-1);
    cin>>t;
    while(t--)
    {
        cin>>l>>r;
        cout<<solve(r)-solve(l-1)<<endl;
    }
    return 0;
}
View Code

例题6:

原文地址:https://www.cnblogs.com/widsom/p/7380612.html