HDU 3652 B-number(数位DP)
2016-03-14 22:28
411 查看
题目链接:点击打开链接
题意:给你一个数n, 求从1到n有多少个数满足:包含13这个子串并且可以被13整除。
思路:又是一道典型的数位DP水题, 套路是一样的, 用d[i][last][p][cur][res]表示当前到了第i位, 上一位是last, 等于还是小于n, 当前是否已经包含13这个子串, 当前的余数为res的最终答案。 递推即可。
细节参见代码:
题意:给你一个数n, 求从1到n有多少个数满足:包含13这个子串并且可以被13整除。
思路:又是一道典型的数位DP水题, 套路是一样的, 用d[i][last][p][cur][res]表示当前到了第i位, 上一位是last, 等于还是小于n, 当前是否已经包含13这个子串, 当前的余数为res的最终答案。 递推即可。
细节参见代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; typedef long long ll; typedef long double ld; const ld eps = 1e-9, PI = 3.1415926535897932384626433832795; const int mod = 1000000000 + 7; const int INF = 0x3f3f3f3f; // & 0x7FFFFFFF const int seed = 131; const ll INF64 = ll(1e18); const int maxn = 10 + 10; int T,n,m,len,d[maxn][maxn][2][2][maxn], vis[maxn][maxn][2][2][maxn], kase=0; char s[maxn]; int dp(int i, int last, int p, int cur, int res) { int& ans = d[i][last][p][cur][res]; if(i == len) return (cur && !res); if(vis[i][last][p][cur][res] == kase) return ans; vis[i][last][p][cur][res] = kase; ans = 0; int v = s[i] - '0'; for(int j=0;j<10;j++) { if(p == 0) { if(j == v) { if(last == 1 && j == 3) ans += dp(i+1, j, 0, 1, (res*10 + j)%13); else ans += dp(i+1, j, 0, cur, (res*10 + j)%13); } else if(j < v) { if(last == 1 && j == 3) ans += dp(i+1, j, 1, 1, (res*10 + j)%13); else ans += dp(i+1, j, 1, cur, (res*10 + j)%13); } } else { if(last == 1 && j == 3) ans += dp(i+1, j, 1, 1, (res*10 + j)%13); else ans += dp(i+1, j, 1, cur, (res*10 + j)%13); } } return ans; } int main() { while(~scanf("%s",s)) { ++kase; len = strlen(s); printf("%d\n",dp(0, 0, 0, 0, 0)); } return 0; }
相关文章推荐
- 创建私有CA
- DIV+CSS 网页布局之:混合布局
- 在xib和story board上实现代码自动布局(解决添加约束时,大于等于小于等于繁琐的问题,以及占位view的问题)
- jQuery 中 children() 与 find() 用法的区别
- 欧拉函数
- Kubernetes中的垃圾回收机制
- Repeater分页
- 求最小公倍数
- 特征提取算法--Sift
- C++笔试
- java学习笔记-接口与内部类
- php 类文件
- shell 之 2>&1 和 tee
- 第三周第四项目——穷举法
- 移动无边框窗体(设置标志位更流畅,或者发送WM_SYSCOMMAND和SC_MOVE + HTCAPTION消息)
- 额。。。c++ sort()排序问题
- shell脚本的作用,shell脚本变量的定义和三种赋值方式【shell脚本的学习笔记一】
- jdbc管理事务以及事务隔离级别的概念
- C语言中的技术陷阱
- 51nod--1265 四点共面 (计算几何基础, 点积, 叉积)