首页 > 学院 > 开发设计 > 正文

BZOJ1026: [SCOI2009]windy数(数位dp)

2019-11-14 12:04:44
字体:
来源:转载
供稿:网友
Description  windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数?Input  包含两个整数,A B。Output  一个整数Sample Input【输入样例一】1 10【输入样例二】25 50Sample Output【输出样例一】9【输出样例二】20HINT【数据规模和约定】100%的数据,满足 1 <= A <= B <= 2000000000 。dp[i][j]表示长度为i,最高位为0的数的windy数的个数。这道题是不计前导零的,由于第一次接触这个,卡在这里好久了。果然还是要多多刷题。题意很简单,不过值得思考,加油,先睡了。/************************************************************** PRoblem: 1026 User: _ostreamBaba Language: C++ Result: Accepted Time:0 ms Memory:1296 kb****************************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<cctype>#include<cmath>#include<ctime>#include<string>#include<stack>#include<deque>#include<queue>#include<list>#include<set>#include<map>#include<cstdio>#include<limits.h>#define MOD 1000000007#define fir first#define sec second#define fin freopen("H://input.txt", "r", stdin)#define fout freopen("H://output.txt", "w", stdout)#define mes(x, m) memset(x, m, sizeof(x))#define Pii pair<int, int>#define Pll pair<ll, ll>#define INF 1e9+7#define inf 0x3f3f3f3f#define Pi 4.0*atan(1.0)#define lowbit(x) (x&(-x))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long ll;typedef unsigned long long ull;const double eps = 1e-12;const int maxn = 36;using namespace std;inline int read(){ int x(0),f(1); char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}ll dp[20][10];int bit[20];int k;inline ll dfs(int pos,int extra,bool flag,bool zero){ if(pos==0){ return 1; } if(!zero&&!flag&&~dp[pos][extra]){ return dp[pos][extra]; } ll ret=0; int x=flag?bit[pos]:9; for(int i=0;i<=x;++i){ if(zero||(!zero&&abs(extra-i)>1)){ ret+=dfs(pos-1,i,flag&&i==bit[pos],zero&&!i); } } if(!flag&&!zero){ dp[pos][extra]=ret; } return ret;}ll cacl(ll n){ k=0; while(n){ bit[++k]=n%10; n/=10; } return dfs(k,0,true,true);}int main(){ mes(dp,-1); ll l,r; while(~scanf("%lld%lld",&l,&r)){ printf("%lld/n",cacl(r)-cacl(l-1));} return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表