zoukankan      html  css  js  c++  java
  • BZOJ 1026 windy数 (数位DP)

    题意

    区间[A,B]上,总共有多少个不含前导零且相邻两个数字之差至少为2的正整数?

    思路

    状态设计非常简单,只需要pos、limit和一个前驱数pre就可以了,每次枚举当前位时判断是否与上一位相差2即可。一个需要注意的地方是第一位不用比较,所以我们还需要一个flag标志记录当前pos位是不是第一位。

    代码

      [cpp] #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <set> #include <stack> #include <queue> #define MID(x,y) ((x+y)/2) #define MEM(a,b) memset(a,b,sizeof(a)) #define REP(i, begin, m) for (int i = begin; i < begin+m; i ++) using namespace std; typedef long long LL; typedef vector <int> VI; typedef set <int> SETI; typedef queue <int> QI; typedef stack <int> SI; const int oo = 0x7fffffff; int dp[12][10][2]; VI num; int dfs(int pos, int pre, bool flag, int limit){ if (pos == -1) return 1; if (!limit && ~dp[pos][pre][flag]) return dp[pos][pre][flag]; int end = limit?num[pos]:9, res = 0; for (int i = 0; i <= end; i ++){ if (flag || abs(i - pre) >= 2){ res += dfs(pos-1, i, flag&&(i == 0),limit&&(i==end)); } } return limit?res:dp[pos][pre][flag] = res; } int cal(int x){ num.clear(); while(x){ num.push_back(x%10); x /= 10; } return dfs(num.size()-1, 0, 1, 1); } int main(){ int a,b; MEM(dp, -1); while(scanf("%d %d", &a, &b) != EOF){ printf("%d ", cal(b)-cal(a-1)); } return 0; } [/cpp]
  • 相关阅读:
    二柱子在线答题
    SWUST OJ(952)
    SWUST OJ (943)
    FileZilla 客户端连接 FlieZilla 服务器 连接成功读取目录列表却失败的解决办法
    串的模式匹配算法 ------ KMP算法
    lvalue require as increment operand
    c 语言连续输入字符型数据
    [pat]数素数
    [PAT]数字分类
    HDOJ_4540_威威猫系列故事——打地鼠
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114315.html
Copyright © 2011-2022 走看看