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

    题目链接:https://www.luogu.com.cn/problem/P2657

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 30;
    
    ll l, r;
    ll dp[maxn][maxn];
    
    vector<int> dig;
    
    ll dfs(int pos, int pre, int lead, int limit){
    	if(!pos) return 1;
    	if(!lead && !limit && dp[pos][pre] != -1) return dp[pos][pre];
    	
    	int lim = limit ? dig[pos] : 9;
    	ll res = 0;
    	for(int i = 0 ; i <= lim ; ++i){
    		if(abs(pre-i) < 2) continue;
    		if(lead && i == 0) { // 依旧是前导零 
    			res += dfs(pos-1, 11, 1, limit & (i==lim));
    		} else{
    			res += dfs(pos-1, i, 0, limit & (i == lim));
    		}
    	}
    	
    	if(!lead && !limit) dp[pos][pre] = res;
    	
    	return res;
    }
    
    ll solve(ll x){
    	dig.clear();
    	memset(dp, -1, sizeof(dp));
    	
    	dig.push_back(-1);
    	ll tmp = x;
    	while(tmp){
    		dig.push_back(tmp % 10);
    		tmp /= 10;
    	}
    	
    	return dfs(dig.size()-1, 11, 1, 1);
    }
    
    ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
    
    int main(){
    	l = read(), r = read();
    	
    	printf("%lld
    ", solve(r) - solve(l-1));
    	
    	return 0;
    }
    
  • 相关阅读:
    网络设备
    Linux常用操作
    工作常用笔记
    性能测试问题总结
    mysql性能分析
    Java8新特性学习笔记-CompletableFuture
    Java语言定义的线程状态分析
    Gatling-插件开发
    RabbitMQ记录
    Go常用库
  • 原文地址:https://www.cnblogs.com/tuchen/p/15140950.html
Copyright © 2011-2022 走看看