摘要: 题意:求区间[x , y]中beautiful number的个数,a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.分析:一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而1到9的最小公倍数为2520,数位DP时我们只需保存前面那些位的最小公倍数就可进行状态转移,到边界时就把所有位的lcm求出了,为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,显然要记录值是不可能的,其实我们只需记录它对2520的模即可,这样我们就可以设 阅读全文
posted @ 2012-09-02 22:33 BeatLJ 阅读(1984) 评论(0) 推荐(0) 编辑
摘要: 题意:给m个字符串,求长为n且至少包含k个上述字符串的字符串有多少个。数据范围:(1<=n<=25),(0<=m<=10)分析:用dp[i][cur][s]表示走i步后,到达结点cur,包含的字符串压缩为状态s(第x位为1表示包含第x个字符串)的字符串有多少个。TLE:当dp[i-1][pre][s]=0时,直接continue,不用进入下一层循环,经此优化后就AC了。View Code #include <stdio.h>#include <string.h>#include <queue>using namespace std;# 阅读全文
posted @ 2012-09-02 17:45 BeatLJ 阅读(317) 评论(0) 推荐(0) 编辑
摘要: 题意:给n个01字符串,求区间[x , y]中有多少个数写成BCD码后不包含以上01串。分析:先用01字符串建立AC自动机(注意标记危险结点),然后DP。dp[i][s]表示扫描前i位后有多少个数会到达自动机的结点s.TLE:数位DP写搓了……(为此今天专门学习了下数位DP的dfs写法,发现确实比递推的快不少)WA:1、高精度减1写错了; 2、由于有取模,所以cal(y)可能小于cal(x-1),输出的时候没考虑到。View Code #include <stdio.h>#include <string.h>#include <queue>using nam 阅读全文
posted @ 2012-09-02 16:43 BeatLJ 阅读(377) 评论(0) 推荐(0) 编辑
摘要: 题意:求[x , y]中有多少个平衡数。平衡数:选定一位作为支点,若左右的力矩平衡则该数是平衡数,否则不是。For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 4*2 + 1*1 = 9 and 9*1 = 9View Code #include <stdio.h>#include <string.h>#define N 19#define M 1378typedef __int64 LL;LL dp[N][N][M];int digit[N];LL dfs(in 阅读全文
posted @ 2012-09-02 16:26 BeatLJ 阅读(422) 评论(0) 推荐(0) 编辑
摘要: 题意:求1-n中有多少个数含字符串”13"且能被13整除。(1 <= n <= 1000000000).练习数位DP的dfs写法。View Code #include <stdio.h>#include <string.h>#define N 10int dp[N][13][3],digit[N];int dfs(int pos,int r,int s,int f){ if(pos==-1) return r==0&&s==2; if(!f&&dp[pos][r][s]!=-1) return dp[pos][r][ 阅读全文
posted @ 2012-09-02 16:21 BeatLJ 阅读(207) 评论(0) 推荐(0) 编辑
摘要: 题意:求1-N中有多少个数包含字符串"49"。1 <= N <= 2^63-1重写这题就是为了学习数位DP的dfs写法,感觉dfs的写法思路更清晰,速度也更快。View Code #include <stdio.h>#include <string.h>#define N 20typedef __int64 LL;LL dp[N][3];int digit[N];LL dfs(int pos,int s,int f){ if(pos==-1) return s==2; if(!f&&dp[pos][s]!=-1) retu 阅读全文
posted @ 2012-09-02 16:18 BeatLJ 阅读(225) 评论(0) 推荐(0) 编辑