zoukankan      html  css  js  c++  java
  • BZOJ1026[SCOI2009]windy数

    @(BZOJ)[数位DP]

    题面

    Description

    windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数?

    Input

    包含两个整数,A B。

    Output

    一个整数

    Sample Input

    【输入样例一】

    1 10
    

    【输入样例二】

    25 50
    

    Sample Output

    【输出样例一】

    9
    

    【输出样例二】

    20
    

    HINT

    【数据规模和约定】
    100%的数据,满足 1 <= A <= B <= 2000000000 。

    Solution

    我们记f[i][j][k = 0 或 1]表示从(0)到总共有(i)位且最高位为(j)的数中, 有多少个Windy数. (k)表示是否存在前导0. 记忆化搜索即可.

    #include <cstdio>
    #include <cstring> 
    #include <algorithm>
    
    int f[10][10][2], dgt[10];
    
    int DFS(int pos, int cur, bool bnd, bool flg)
    {	
    	if(! pos)
    		return 1;
    
    	int res = 0;
    
    	if(! bnd)
    	{
    		if(~ f[pos][cur][flg])
    			return f[pos][cur][flg];
    		
    		for(int i = 0; i < 10; ++ i)
    			if(! flg || abs(cur - i) >= 2)
    				res += DFS(pos - 1, i, 0, flg || (bool)i);	
    
    		return f[pos][cur][flg] = res;
    	} 
    	
    	for(int i = 0; i < dgt[pos - 1]; ++ i)
    		if(abs(cur - i) >= 2)
    			res += DFS(pos - 1, i, 0, 1);
    	
    	if(abs(cur - dgt[pos - 1]) >= 2)
    		res += DFS(pos - 1, dgt[pos - 1], 1, 1);
    	
    	return res;
    }
    
    inline int calculate(int a)
    {
    	if(! a)
    		return 1;
    	
    	int cnt = 0;
    	
    	for(; a; dgt[cnt ++] = a % 10, a /= 10);
    	
    	int res = 0;
    	
    	for(int i = 0; i < dgt[cnt - 1]; ++ i)
    		res += DFS(cnt - 1, i,  0, (bool)i);
    	
    	return res += DFS(cnt - 1, dgt[cnt - 1], 1, 1);
    }
    
    int main()
    {
    	#ifndef ONLINE_JUDGE
    	freopen("BZOJ1026.in", "r", stdin);
    	freopen("BZOJ1026.out", "w", stdout);
    	#endif 
    	
    	int a, b;
    	scanf("%d%d", &a, &b);
    	memset(f, -1, sizeof(f));
    	printf("%d
    ", calculate(b) - calculate(a - 1));
    }
    
  • 相关阅读:
    贪心法
    div 样式
    echarts标题(title)配置
    利用svg画路径图 vue
    vue 杂项
    Charset 0x0408D00000/MS936 is not supported by the JVM
    Android开发中Eclipse常用快捷键
    Java 中强制删除文件的方法
    利用html5的localStorage结合jquery实现日常费用查询器
    Ant编译utf8非法字符:/65279 解决方法
  • 原文地址:https://www.cnblogs.com/ZeonfaiHo/p/6773528.html
Copyright © 2011-2022 走看看