首页 > 代码库 > USETC 250 windy数 数位DP
USETC 250 windy数 数位DP
注意处理数字只有一位的情况(其实不用怎么处理)= =
简单数位DP
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 20;int lim[maxn],len;int num[maxn];int f[15][15][15][2];void getlim(int num) { memset(lim,0,sizeof(lim)); len = 0; while(num) { lim[len++] = num % 10; num /= 10; }}int myabs(int x) { return x < 0 ? -x : x;}int dfs(int now,int prev,int maxd,int first,int bound) { int ¬e = f[now][prev][maxd][first]; if(now == 0) { if(first && maxd >= 2) { return 1; } return 0; } if(!bound && note != -1) return note; int m = bound ? lim[now - 1] : 9,ret = 0; for(int i = 0;i <= m;i++) { num[now - 1] = i; if(first) { int nmaxd = min(maxd,myabs(i - prev)); ret += dfs(now - 1,i,nmaxd,1,bound && i == m); } else { ret += dfs(now - 1,i,maxd,i || first,bound && i == m); } } if(!bound) note = ret; return ret;}int work(int num) { memset(f,-1,sizeof(f)); if(num < 10) return num; getlim(num); return dfs(len,0,11,0,1);}int main() { int n,m; while(scanf("%d%d",&n,&m) == 2) { printf("%d\n",work(m) - work(n - 1)); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。