zoukankan      html  css  js  c++  java
  • 【BZOJ】1026: [SCOI2009]windy数(数位dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1026

    我果然很弱啊。。。

    考虑数位dp。枚举每一位,然后限制下一位即可。

    一定要注意啊!在dfs的时候line这个要&&啊。。。。要不然wa了两发。。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << (#x) << " = " << (x) << endl
    #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
    #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '	'; cout << endl
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    
    const int N=25;
    int a[N], d[N][10], len;
    int dfs(int x, int num, int front, int line) {
    	if(!x) {
    		if(front) return 0; //注意前导0不要,其实要不要都可以a。。。因为重复的会被剪掉。。
    		return 1;
    	}
    	if(!front && !line && d[x][num]!=-1) return d[x][num];
    	int last=9, tot=0; if(line) last=a[x];
    	for1(i, 0, last) {
    		if(front) {
    			if(i==0) tot+=dfs(x-1, i, 1, line && i==last); //这个line一定要并上啊QAQ
    			else tot+=dfs(x-1, i, 0, line && i==last);
    		}
    		else if(abs(i-num)>=2) tot+=dfs(x-1, i, 0, line && i==last);
    	}
    	if(!front && !line) d[x][num]=tot;
    	return tot;
    }
    int getans(int x) {
    	len=0;
    	while(x) a[++len]=x%10, x/=10;
    	return dfs(len, 0, 1, 1);
    }
    int main() {
    	CC(d, -1);
    	int x=getint(), y=getint();
    	print(getans(y)-getans(x-1));
    	return 0;
    }
    

    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 。

    Source

  • 相关阅读:
    [shell]Shell经常使用特殊符号
    谈谈大三找暑假实习
    使用zTree控件制作的表格形式的树形+数据菜单
    Bestcoder #47 B Senior&#39;s Gun
    使用awrextr.sql导出awr原始数据
    linux/shell 文本文件删除/删掉空行
    python 统计文本文件的行数
    check if a linux process is done using bash 检查进程是否在运行
    umount移动硬盘遇到device is busy问题
    Python读写文件
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/4006193.html
Copyright © 2011-2022 走看看