1. 程式人生 > >BZOJ 1026 windy數【數位DP】

BZOJ 1026 windy數【數位DP】

ctype ret += pac rst true bug ring fine

1026: [SCOI2009]windy數

Time Limit: 1 Sec Memory Limit: 162 MB
Submit: 10142 Solved: 4712
[Submit][Status][Discuss]

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 。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include
<algorithm> #include<vector> #include<map> #include<cctype> #include<stack> #include<sstream> #include<list> #include<assert.h> #include<bitset> #include<numeric> #define debug() puts("++++") #define gcd(a,b) __gcd(a,b) #define lson l,m,rt<<1 #define
rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a,b,sizeof(a)) #define sz size() #define be begin() #define pu push_up #define pd push_down #define cl clear() #define lowbit(x) -x&x #define all 1,n,1 #define rep(i,x,n) for(int i=(x); i<=(n); i++) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e18; const int maxm = 1e6 + 10; const double PI = acos(-1.0); const double eps = 1e-8; const int dx[] = {-1,1,0,0,1,1,-1,-1}; const int dy[] = {0,0,1,-1,1,-1,1,-1}; int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int mod = 10056; #define inf 0x3f3f3f3f #define ll long long const int maxn = 150; int digit[20]; //49 //62 \ 4 ll dp[20][12]; //第一個參數:數位 第二個參數: //對每個數位上的狀態進行dfs,模擬正常數數的過程 //if6——》(狀態)當前數是否是4,他的上一位是否是4 ll dfs(int len, int last ,bool limit)//limit代表他的上一位是否是上界 { int p; if(len<=0) return 1;//個位 if(!limit && dp[len][last]!=-1 && last>=0 ) return dp[len][last];//5123 還沒有到5——0 1 2 3 4 //記憶化搜索 ll cnt=0,up_bound=(limit?digit[len]:9); for(int i=0; i<=up_bound; i++) { if(abs(i-last)<2) continue; //剪62枝 p=i; if(i==0 && last==-10) p=last; cnt += dfs(len-1, p, limit && i==up_bound); } if(!limit && last>=0) dp[len][last]=cnt; //完整狀態 return cnt; } ll solve(ll num) { int k=0; //記錄有多少個數位 while(num) { digit[++k]=num%10; num/=10; } ms(dp,255); return dfs(k,-10,true); } int main() { int t; ll n,m; while(~scanf("%lld%lld",&n,&m)) { printf("%lld\n",solve(m)-solve(n-1)); } } /* 【輸入樣例一】 1 10 9 25 50 20 */

BZOJ 1026 windy數【數位DP】