BZOJ1026 [SCOI2009]windy数(数位DP)

/**************************************************************
    Problem: 1026
    User: wrjlinkkkkkk
    Language: C++
    Result: Accepted
    Time:60 ms
    Memory:1288 kb
****************************************************************/
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
     
#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) 
 
using namespace std;
 
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;
 
const db eps = 1e-6;
const int mod = 100003;
const int maxn = 2e5+100;
const int maxm = 2e5+100;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const db pi = acos(-1.0);
 
int f[20][20];//首位为j的i位数的所有数中windy数的个数
int sum[20];//不含前导零的i位windy数
int st[20];//数的每一位
int top;
 
int solve(int x){
    int ans = 0;
    int top = 0;
    int p = 1;
    while(x){
        st[++top] = x%10;
        ans += sum[p-1];
        p++;
        x /= 10;
    }
     
    for(int i = 1; i < st[top]; i++){
        ans += f[top][i];
    }
    for(int i = top-1; i > 0; i--){
        for(int j = 0; j < st[i]; j++){
            if(abs(j-st[i+1])<2)continue;
            ans += f[i][j];
        }
        if(abs(st[i]-st[i+1])<2)break;
    }
    return ans;
}
 
int main() {
    int A, B;
    scanf("%d %d", &A, &B);
    mem(f, 0);
    mem(sum, 0);
    for(int i = 0; i < 10; i++) f[1][i] = 1;
    for(int i = 2; i <= 10; i++){
        for(int j = 0; j < 10; j++){
            for(int k = 0; k < 10; k++){
                if(abs(j-k)<2)continue;
                f[i][j] += f[i-1][k];
            }
        }
    }
    for(int i = 1; i <= 10; i++){
        for(int j = 1; j <= 10; j++){
            sum[i] += f[i][j];
        }
    }
    printf("%d", solve(B+1)-solve(A));
    return 0;
}

 

posted @ 2018-10-02 22:43  wrjlinkkkkkk  阅读(197)  评论(0编辑  收藏  举报