zoukankan      html  css  js  c++  java
  • 【SCOI 2009】 Windy数

    【题目链接】

                点击打开链接

    【算法】

              数位DP,注意处理前导零的情况

    【代码】

               

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXL 15
    
    int n,m;
    int dp[MAXL][10],a[MAXL];
    
    template <typename T> inline void read(T &x) {
            int f = 1; x = 0;
            char c = getchar();
            for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
            for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
            x *= f;
    }
    template <typename T> inline void write(T x) {
            if (x < 0) { x = -x; putchar('-'); }
            if (x > 9) write(x/10);
            putchar(x%10+'0');
    }
    template <typename T> inline void writeln(T x) {
            write(x);
            puts("");
    }
    inline void getdp() {
            int i,j,k;
            for (i = 0; i <= 9; i++) dp[1][i] = 1;
            for (i = 2; i <= MAXL; i++) {
                    for (j = 0; j <= 9; j++) {
                            for (k = 0; k <= 9; k++) {
                                    if (abs(j-k) >= 2) dp[i][j] += dp[i-1][k]; 
                            }
                    }
            }
    }
    inline int calc(int n) {
            int i,j,ans = 0;
            a[0] = 0;
            while (n) {
                    a[++a[0]] = n % 10;
                    n /= 10;
            }    
            for (i = 1; i < a[0]; i++) {
                    for (j = 1; j <= 9; j++) {
                            ans += dp[i][j];
                    }
            }
            for (i = a[0]; i >= 1; i--) {
                    for (j = 0; j < a[i]; j++) {
                            if (i == a[0] && !j) continue;
                            if ((i == a[0]) || (abs(j-a[i+1]) >= 2))
                                    ans += dp[i][j];
                    }
                    if (i != a[0] && abs(a[i]-a[i+1]) < 2) break;
            }
            return ans;
    }
    
    int main() {
        
            getdp();
            read(n); read(m);
            writeln(calc(m+1)-calc(n));
            
            return 0;
    } 
  • 相关阅读:
    【转】Testing, Thought, and Observations
    【转】Yet another software testing pyramid
    【转】Automated Testing and the Test Pyramid
    Environment setting up troubleshoot.
    [转] Spring相关
    流水账
    Abbreviation
    chrome中文本框样式问题
    Intellij IDEA 使用总结
    限制
  • 原文地址:https://www.cnblogs.com/evenbao/p/9196366.html
Copyright © 2011-2022 走看看