zoukankan      html  css  js  c++  java
  • windy数,数位dp

    中文题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1026

    用着这样的数位dp姿势搞下,就好了。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <map>
    #include <set>
    using namespace std;
    #define INF 1000000000
    
    
    int dp[20][20][2];
    
    int pos[20];
    int judge(int x, int y)
    {
    	if (abs(x - y) >= 2) return 1;
    	return 0;
    }
    
    int nong(int x, int last, int first, int flag)
    {
    	int ans = 0;
    	if (x == 0) return 1;
    	if (!flag&&~dp[x][last][first]) return dp[x][last][first];
    	int bound = flag ? pos[x] : 9;
    	for (int i = 0; i <= bound; i++){
    		if (first == 0){
    			ans += nong(x - 1, i, first || i, flag&&i == bound);
    		}
    		else{
    			if (judge(i, last)) ans += nong(x - 1, i, first || i, flag&&i == bound);
    		}
    	}
    	if (!flag) dp[x][last][first] = ans;
    	return ans;
    }
    
    int gao(int x)
    {
    	int len = 0;
    	while (x){
    		pos[++len] = x % 10;
    		x /= 10;
    	}
    	return nong(len, 0, 0, 1);
    }
    
    
    int main()
    {
    	int A, B;
    	memset(dp, -1, sizeof(dp));
    	while (cin >> A >> B){
    		cout << gao(B) - gao(A - 1) << endl;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    【javascript】select操作实例
    【javascript】函数
    【javascript】一些资料
    【javascript】操作符:一元操作符
    动态执行Sql
    索引( index )
    事务
    用户相关
    视图(view)
    函数(function)
  • 原文地址:https://www.cnblogs.com/yigexigua/p/4384475.html
Copyright © 2011-2022 走看看