日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HYSBZ - 1026 windy数(数位dp)

發布時間:2024/4/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HYSBZ - 1026 windy数(数位dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:求a到b閉區間內滿足相鄰兩個數字之差大于等于2的數字個數

分析:dp[i][j]表示第i位上數字為j的結果,pos表示位數,pre表示上一位數,lead表示是否有前導0,limit表示是否為最高位

代碼:

#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<sstream> #include<cmath> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=30;LL dp[20][10];int b[N];LL dfs(int pos,int pre,bool lead,bool limit) {if(pos==-1)return 1;if(!limit&&dp[pos][pre]!=-1&&pre>0)//pre>0,如果pre==0,涉及到位數,需要再次循環return dp[pos][pre];int up=limit?b[pos]:9;LL ans=0;for(int i=0;i<=up;i++){if(abs(i-pre)>=2||lead)ans+=dfs(pos-1,i,lead&&(i==0),limit&&(i==b[pos]));}if(!limit)dp[pos][pre]=ans;return ans; }LL solve(LL n) {int cnt=0;while(n){b[cnt++]=n%10;n/=10;}return dfs(cnt-1,-1,true,true); }int main() { // freopen("input.txt","r",stdin);LL a,b;memset(dp,-1,sizeof(dp));while(cin>>a>>b){cout<<solve(b)-solve(a-1)<<endl;}return 0; }

?

總結

以上是生活随笔為你收集整理的HYSBZ - 1026 windy数(数位dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。