做法:按位暴力DFS吧...
這裡很明顯沒有考慮當前數字是否是首位,其實不需要啊,因為這種情況發生的時候肯定是那種比邊界數少一位的數字。也就是說如果求1234 那麼這裡要考慮的是 0 ---999的
數字中的mountain, 沒有考慮首位的後果是moutain的形態不同,可是很容易證明,規定位元的數字只有兩種moutain,而這種兩種moutain在 0---999中的個數相同。
#include <iostream>#include <cstdio>#include <cstring>#define LL long longconst int LMT=11;using namespace std;LL dp[LMT][LMT][LMT];int sec[LMT];LL dfs(int pre,int pos,int len,bool tag){ if(dp[pre][pos][len]!=-1&&(!tag||pos==len))return dp[pre][pos][len]; LL res=0; int end=tag?sec[pos]:9; if(pos&1) { for(int j=end;j>=pre;j--) res+=dfs(j,pos+1,len,tag&&j==end); } else { for(int j=min(end,pre);j>=0;j--) res+=dfs(j,pos+1,len,tag&&j==end); } if(!tag)dp[pre][pos][len]=res; return res;}int work(LL x){ int num[LMT],res=0; do { num[res]=x%10; res++; } while(x/=10); for(int i=0,j=res-1;j>=0;j--,i++) sec[i]=num[j]; return res;}int main(void){ int T; scanf("%d",&T); memset(dp,-1,sizeof(dp)); for(int i=0;i<LMT;i++) for(int j=0;j<LMT;j++) dp[i][j][j]=1; while(T--) { LL a,b,x,y; scanf("%I64d%I64d",&a,&b); x=dfs(9,0,work(a-1),1); y=dfs(9,0,work(b),1); printf("%I64d\n",y-x); } return 0;}