(原创)数位DP专题小结--by sgx

来源:互联网 发布:unity3d导入max文件 编辑:程序博客网 时间:2024/05/23 07:23
数位DP,一句话概括,就是在一个给定区间内求出满足某中奇葩条件的数字个数,这真是奇葩题目,但是总体写起来又有一定规律性。
主要可以分为以下几个步骤:

确定主体框架,确定一个大方向,想想该如何设计状态;
下面基本就是模板,直接DFS就行了,一位一位处理,这也是他叫按位DP的原因。
数位DP代码一般都很短,不过效率挺好,解决一些竞赛中出现的问题非常有用 。

如果看了这部分 ,你感觉还是不会的话,(这是当然啊,狂汗~~),那么请继续往下看。
下面用几个例子来说明一下,具体注释都附在代码内:

PS:我是菜狗,这些都是很水的数位DP,求大神勿喷~~

————————————————————— ——华丽的分割线—————————————————————


Problem 1160 - 科协的数字游戏I
Time Limit: 1000MS      Memory Limit: 65536KB     Difficulty:    
Total Submit: 181     Accepted: 29     Special Judge: No 

Description

科协里最近很流行数字游戏。某人命名了一种不降数,这种数字必须满足从左到右各位数字成大于等于的关系,如123,446。现在大家决定玩一个游戏,指定一个整数闭区间[a,b],问这个区间内有多少个不降数。

Input

题目有多组测试数据。每组只含2个数字a, b (1 <= a, b <= 2^31)。

Output

每行给出一个测试数据的答案,即[a, b]之间有多少阶梯数。

Sample Input

1 9
1 19

Sample Output

9
18

Hint

 

/********************************************************************* Problem:科协的数字游戏2* source:XDOJ* author:sgx* date:2013/09/15*********************************************************************/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define LL long longconst int N=25;int digit[N];LL dp[N][N];LL dfs(int pos,int statu,int limit){     int i,end,s;     LL res=0;     if(pos==-1)        return 1;     if(!limit&&dp[pos][statu]!=-1)        return dp[pos][statu];     end=limit?digit[pos]:9;     for(i=statu;i<=end;i++)         res+=dfs(pos-1,i,limit&&i==end);     if(!limit)         dp[pos][statu]=res;    return res;}LL calc(LL n){     int len=0;     memset(dp,-1,sizeof(dp));     while(n)     {         digit[len++]=n%10;         n/=10;     }     return dfs(len-1,0,1);}int main(){     LL a,b;     while(scanf("%lld %lld",&a,&b)!=EOF)         printf("%lld\n",calc(b)-calc(a-1));    return 0;}


 

————————————————————————————————————分割线—————————————————————————————————————

Problem 1161 - 科协的数字游戏II

Time Limit: 1000MS     Memory Limit: 65536KB     Difficulty:    
Total Submit: 108    Accepted: 13    Special Judge: No 
Description

由于科协里最近真的很流行数字游戏。(= =!汗一个)某人又命名了一种取模数,这种数字必须满足各位数字之和 mod N为0。现在大家又要玩游戏了,指定一个整数闭区间[a,b],问这个区间内有多少个取模数。

Input
题目有多组测试数据。每组只含3个数字a, b, n (1 <= a, b <= 2^31,1 <= n < 100)。
Output
每个测试用例输出一行,表示各位数字和 mod N为0 的数的个数。
Sample Input
1 19 9
Sample Output
2
Hint

Source
tclh123

 

 

/********************************************************************* Problem:科协的数字游戏2* source:XDOJ* 分析:记忆化搜索部分,pre表示前面各位数字之和对该数取模的结果* author:sgx* date:2013/09/15*********************************************************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int maxn=100+5;int dp[maxn][105];int digit[maxn];int mod,l,r;int DFS(int pos,int pre,bool limit){    if(pos==-1)        return pre==0;    if(!limit&&dp[pos][pre]!=-1)        return dp[pos][pre];     LL res=0,end=limit?digit[pos]:9;     for(int i=0;i<=end;i++)     {        int new_pre=(pre+i)%mod;         res+=DFS(pos-1,new_pre,limit&&i==end);     }     if(!limit)         dp[pos][pre]=res;    return res;}LL solve(int n){     int len=0;     while(n)     {         digit[len++]=n%10;         n/=10;     }     return DFS(len-1,0,true);}int main(){     while(scanf("%d%d%d",&l,&r,&mod)!=EOF)     {         memset(dp,-1,sizeof(dp));         printf("%lld\n",solve(r)-solve(l-1));     }     return 0;}


 


————————————分割线————————

HDU3052--B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1556    Accepted Submission(s): 852
Problem Description

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Output
Print each answer in a single line.
Sample Input
13
100
200
1000
Sample Output
1
1
2
2

 

 

/*************************************************************** 题意:求[1,n]内有多少个数字,该数字有13,且能被13整除 n<=10^9即要满足x % 13 = 0;x=pre*10^pos+next; (pos代表处理到当前的位数,next代表正在处理的位置上面的数字) (pre*10^pos + next) % 13 = 0,pre是之前确定的部分; 需要的参数为pre , pos ,状态用status表示status==2记录pre拥有"13",status==1是表示没有出现13,但是首位是1;status=0表示没有13;* Author:sgx* Date:2013/09/15*************************************************************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int maxn=100+5;int dp[10][13][3];int digit[10];int DFS(int pos,int status,int pre,bool limit){    if(pos==-1)        return status==2&&pre==0;    if(!limit&&dp[pos][pre][status]!=-1)        return dp[pos][pre][status];     LL res=0;     int end=limit?digit[pos]:9;     for(int i=0;i<=end;i++)     {        int new_pre=(pre*10+i)%13;        int new_status=status;        /*准备计算出下一阶段中新的状态status*/        if(status==0&&i==1)             new_status=1;/*原来没有出现13但是当前位是1,所以属于状态1对应的情况,故更新新状态为1;*/        if(status==1&&i==1)             new_status=1;/*解释方法同上*/        else if(status==1&&i!=3)             new_status=0;        if(status==1&&i==3)             new_status=2;        res+=DFS(pos-1,new_status,new_pre,limit&&i==end);        /*//limit==true则说明有限制,即所有可能并没有被全部记录,故此时记入dp数组 */        //limit==false则说明之后的分支状态已经搜索完全    }    if(!limit)         dp[pos][pre][status]=res;    return res;}LL solve(int n){     int len=0;     while(n)     {         digit[len++]=n%10;         n/=10;     }     return DFS(len-1,0,0,true);}int main(){     LL m,n;     while(scanf("%lld",&n)!=EOF)     {         memset(dp,-1,sizeof(dp));         printf("%lld\n",solve(n));     }     return 0;}


 

HDU3555----BombTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4594    Accepted Submission(s): 1601
Problem Description

The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.
Output
For each test case, output an integer indicating the final points of the power.
Sample Input
3
1
50
500
Sample Output
0
1
15

 

 

/******************************************************************* PS:网上大神的文章真的太神了,改不了,贴过来* 附个链接:http://blog.csdn.net/whyorwhnt/article/details/8764955*******************************************************************/#include <cstdio>#include <cstring>using namespace std;int bit[25];__int64 dp[25][3];//dp[i][0]表示长度为i,没有49//dp[i][1]表示长度为i,没有49但前一位为4//dp[i][2]表示长度为i,包括49的个数/*limit表示是否有上限,比如n=1234,现在转移到12,如果下一位选3,那么再下一位就有上限,上限为4,如果不选3,那么下一位就没限制,最高位9,转移能保证转移到数比n小*/__int64 Dfs (int pos,int s,bool limit) //s为之前数字的状态{     if (pos==-1)        return s==2;     if (limit==false && ~dp[pos][s])        return dp[pos][s];     int i ,end=limit?bit[pos]:9;      __int64 ans=0;     for (i=0;i<=end;i++)     {        int nows=s;        if(s==0 && i==4)             nows=1;        if(s==1 && i!=9) //前一位为4             nows=0;        if(s==1 && i==4)             nows=1;        if(s==1 && i==9) //49             nows=2;         ans+=Dfs(pos-1 , nows , limit && i==end);     }    //limit==true则说明有限制,即所有可能并没有被全部记录,故此时记入dp数组    //limit==false则说明之后的分支状态已经搜索完全    return limit?ans:dp[pos][s]=ans;}int main (){      __int64 n;     int T;     memset(dp,-1,sizeof(dp));     scanf("%d",&T);     while (T--)     {         scanf("%I64d",&n);         int len=0;         while (n)         {             bit[len++]=n%10;             n/=10;         }         printf("%I64d\n",Dfs(len-1,0,1));     }     return 0;}

 

HDU2089--不要62
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13687    Accepted Submission(s): 4402
Problem Description

杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
Sample Input
1 100
0 0
Sample Output
80


 

/********************************************************************* Problem:HDU2089-不要62* source:HDU* 分析:记忆化搜索部分,i==4时要及时continue掉,不然无限WA;* author:sgx* date:2013/09/15*********************************************************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int maxn=100+5;int dp[maxn][3];int digit[maxn];//dp[i][0]表示长度为i,没有62//dp[i][1]表示长度为i,没有62但前一位为6//dp[i][2]表示长度为i,包括62的个数int DFS(int pos,int status,bool limit){    if(pos==-1)        return status==2;    if(!limit&&dp[pos][status]!=-1)        return dp[pos][status];     LL res=0,end=limit?digit[pos]:9;     for(int i=0;i<=end;i++)     {        int new_status=status;        if(i==4)             new_status=2;        else if(status==0&&i==6)             new_status=1;        else if(status==1&&i==6)             new_status=1;        else if(status==1&&i!=2)             new_status=0;        else if(status==1&&i==2)             new_status=2;         res+=DFS(pos-1,new_status,limit&&i==end);     }     if(!limit)         dp[pos][status]=res;     return res;}LL solve(int n){     int len=0;     while(n)     {         digit[len++]=n%10;         n/=10;     }    return DFS(len-1,0,true);}int main(){     int n,m;     while(scanf("%d%d",&n,&m)!=EOF&&(n+m))     {         memset(dp,-1,sizeof(dp));         printf("%lld\n",m-n+1-solve(m)+solve(n-1));     }     return 0;}

 

 

HDU4734F(x)
Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 485    Accepted Submission(s): 179
Problem Description

For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 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 < 109)
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

#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>using namespace std;const int maxn=300;const int maxm=6000;int dp[maxn][maxm];int a[maxn];int DFS(int pos,int cur,int limit){    int i,ed,s,ans=0;    if(pos==-1)        return cur>=0;    if(!limit&&dp[pos][cur]!=-1)        return dp[pos][cur];     ed=limit?a[pos]:9;     for(i=0;i<=ed;i++)     {         s=cur-i*(1<<pos);        if(s<0)            break;         ans+=DFS(pos-1,s,limit&&i==ed);     }     if(!limit&&dp[pos][cur]==-1)         dp[pos][cur]=ans;     return ans;}int transfer(int n){     int res=0,m=1;     while(n)     {         res+=(n%10)*m;         n/=10;         m*=2;     }     return res;}int solve(int n,int m){     int st=-1;     while(m)     {         a[++st]=m%10;         m/=10;     }     int res=DFS(st,transfer(n),1);     return res;}int main(){     int test,n,m;     scanf("%d",&test);     memset(dp,-1,sizeof(dp));     for(int ii=1;ii<=test;ii++)     {         scanf("%d%d",&n,&m);         printf("Case #%d: %d\n",ii,solve(n,m));     }     return 0;}


 

windy数

Description

windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。
windy想知道,在A和B之间,包括A和B,总共有多少个windy数?

Input

包含两个整数,A B。
满足 1 <= A <= B <= 2000000000 。

Output

包含一个整数:闭区间[A,B]上windy数的个数。

Sample Input

1 10


Sample Output

9


纪念UESTC首A,@杜贵平
艰难AC。。。。
/******************************************************************** problem:windy数* source:UESTC1307* author:sgx* date:2013/09/18********************************************************************/#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int maxn=20;typedef long long LL;LL dp[maxn][11];LL digit[maxn];LL DFS(int pos,int pre,bool limit,bool first_place)//first_place判断前导0{    if(pos==-1)        return first_place==0;    if(!limit&&dp[pos][pre]!=-1&&first_place==false)        return dp[pos][pre];     int end=limit?digit[pos]:9;     LL ans=0;     for(int i=0;i<=end;i++)     {        if(first_place!=0)             ans+=DFS(pos-1,i,limit&&i==end,first_place&&i==0);        else if(abs(i-pre)>=2)             ans+=DFS(pos-1,i,limit&&i==end,first_place);     }     if(!limit&&first_place==false)         dp[pos][pre]=ans;     return ans;}LL solve(LL n){     LL len=0;     while(n)     {         digit[len++]=n%10;         n/=10;     }     return DFS(len-1,0,true,true);}int main(){     LL l,r;     while(scanf("%lld%lld",&l,&r)!=EOF)     {         memset(dp,-1,sizeof(dp));         LL ans=solve(r)-solve(l-1);         printf("%lld\n",ans);     }     return 0;}

 
原创粉丝点击