• P2657 [SCOI2009]windy数


    P2657 [SCOI2009]windy数

    题目描述
    windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,

    在A和B之间,包括A和B,总共有多少个windy数?

    Solution

    有先导 (0) 的数位(dp)
    把此位前有无前导 (0) 作为搜索的一个状态即可
    注意有前导 (0) 时不能直接返回, 因为有前导 (0) 就代表着无法到达 (10^{len} - 1)

    Code

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #include<climits>
    #define LL long long
    #define REP(i, x, y) for(LL i = (x);i <= (y);i++)
    using namespace std;
    LL RD(){
        LL out = 0,flag = 1;char c = getchar();
        while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
        while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
        return flag * out;
        }
    const LL maxn = 19;
    LL num[maxn];
    LL dp[maxn][maxn];
    LL DP(LL Index, LL state, LL zero, bool limit){
    	if(Index == 0)return 1;
    	if(!zero && !limit && dp[Index][state] != -1)return dp[Index][state];
    	LL ans = 0, up = limit ? num[Index] : 9;
    	REP(i, 0, up){
    		if(zero)ans += DP(Index - 1, i, i == 0, limit && (i == num[Index]));
    		else{
    			if(abs(i - state) < 2)continue;
    			ans += DP(Index - 1, i, 0, limit && (i == num[Index]));
    			}
    		}
    	if(!zero && !limit)dp[Index][state] = ans;
    	return ans;
    	}
    LL solve(LL x){
    	LL len = 0;
    	while(x){
    		num[++len] = x % 10;
    		x /= 10;
    		}
    	return DP(len, 0, 1, 1);
    	}
    int main(){
    	memset(dp, -1, sizeof(dp));
    	LL l = RD(), r = RD();
    	printf("%lld
    ", solve(r) - solve(l - 1));
    	return 0;
    	}
    
  • 相关阅读:
    vim 显示行号 临时&永久
    ubuntu redis config IP password
    Vulnerability Scanning Tools
    Tomcat清理无用应用防止漏洞
    XSS平台-学习
    Amazon aws s3 加速
    XSS的DOS攻击之 server limit dos
    Linux Nginx naxsi
    Acunetix Web Vulnerability Scanner abbr. AWVS
    nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
  • 原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/9768641.html
Copyright © 2020-2023  润新知