zoukankan      html  css  js  c++  java
  • 洛谷P2657 [SCOI2009]windy数

    P2657 [SCOI2009]windy数

    数位dp模板题,这里要考虑到前导0的情况,考虑一下就行了。
    代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 55;
    ll bit[N] ;
    ll a, b ;
    int tot;
    ll dp[N][2][10][2] ;
    ll ok(ll x, ll y) {
        return abs(x - y) >= 2 ;
    }
    ll dfs(int len, int lim, int last, int lead) {
        if(len == 0) return 1;
        if(!lim && dp[len][lim][last][lead]) return dp[len][lim][last][lead] ;
        ll ans = 0;
        int up = lim ? bit[len] : 9 ;
        if(lead && len != tot) up = 9;
        for(int i = 0; i <= up; i++) {
            if(lead || ok(last, i)) ans += dfs(len - 1, lim & (up == i), i, lead & (i == 0)) ;
        }
        return dp[len][lim][last][lead] = ans ;
    }
    ll solve(ll x) {
        tot = 0;
        while(x) {
            bit[++tot] = x % 10;
            x /= 10;
        }
        return dfs(tot, 1, 20, 1) ;
    }
    int main() {
        cin >> a >> b;
        cout << solve(b) - solve(a - 1);
        return 0;
    }
    
    
  • 相关阅读:
    关于IOS6屏幕旋转的问题
    学习备忘录
    关于多种手势问题
    关于 IOS中 绘制图形的问题
    爱的意义
    IOS支付SDK
    IOS开发特别备注
    ios相关教程
    iOS7光标问题
    收藏网站
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10877585.html
Copyright © 2011-2022 走看看