zoukankan      html  css  js  c++  java
  • bzoj1026 windy数 数位DP

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

    Input

      包含两个整数,A B。

    Output

      一个整数

    Sample Input【输入样例一】 1 10 【输入样例二】 25 50

    Sample Output【输出样例一】 9 【输出样例二】 20Hint

    【数据规模和约定】

    100%的数据,满足 1 <= A <= B <= 2000000000 。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 typedef long long LL;
     6 int bit[20];
     7 LL dp[20][15], n, m;
     8 LL dfs(int pos, int d, int pre, int flag, int limit) {
     9     if (pos == 0 ) return 1;
    10     if ( !flag && !limit && dp[pos][pre] != -1 ) return dp[pos][pre];
    11     int num = limit ? bit[pos] : 9;
    12     LL ans = 0;
    13     for (int i = 0 ; i <= num ; i++) {
    14         if (!flag && abs(pre - i) < 2) continue;
    15         ans += dfs(pos - 1, abs(i - pre), i, flag && i == 0, limit && i == num);
    16     }
    17     if (!limit && !flag) dp[pos][pre] = ans;
    18     return ans;
    19 }
    20 LL solve(LL x) {
    21     int len = 0;
    22     while(x) {
    23         bit[++len] = x % 10;
    24         x /= 10;
    25     }
    26     LL ans = 0;
    27     ans += dfs(len, 30, 0, 1, 1);
    28     return ans ;
    29 }
    30 int main() {
    31     int t;
    32     memset(dp, -1, sizeof(dp));
    33     scanf("%lld%lld", &n, &m);
    34     printf("%lld
    ", solve(m) - solve(n - 1));
    35     return 0;
    36 }
  • 相关阅读:
    Codeforces 451A Game With Sticks
    POJ 3624 Charm Bracelet
    POJ 2127 Greatest Common Increasing Subsequence
    POJ 1458 Common Subsequence
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1698
    HDU 1754
    POJ 1724
    POJ 1201
    CSUOJ 1256
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9345756.html
Copyright © 2011-2022 走看看