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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HDU 3709 Balanced Number (数位DP)

發(fā)布時(shí)間:2023/12/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 3709 Balanced Number (数位DP) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意

求出[x, y] 范圍內(nèi)的平衡數(shù),平衡數(shù)定義為:以數(shù)中某個(gè)位為軸心,兩邊的數(shù)的偏移量為矩,數(shù)位權(quán)重,使得整個(gè)數(shù)平衡。

思路

外層枚舉平衡點(diǎn),然后數(shù)位DP即可。設(shè)計(jì)狀態(tài): dp[pos][o][left_right] 表示處理到當(dāng)前pos位,一開始枚舉o點(diǎn)為支點(diǎn),前pos-1位左邊減右邊的權(quán)值是left_right的數(shù)的個(gè)數(shù)。 注意:1.減法可能為負(fù),所以需要加一個(gè)位移偏量,這里我設(shè)為2000;2.對(duì)于一個(gè)正數(shù),如果它是 Balanced Number,那么它有且只有一個(gè)平衡點(diǎn)。但是計(jì)算0時(shí)枚舉所有的支點(diǎn)都會(huì)把他算在內(nèi)一次,重復(fù)計(jì)算了(位數(shù)-1)次。所以最后結(jié)果要減去。 PS:外層枚舉支點(diǎn)要比內(nèi)層枚舉支點(diǎn)剪枝效果更好。

代碼

? [cpp] #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <set> #include <stack> #include <queue> #define MID(x,y) ((x+y)/2) #define MEM(a,b) memset(a,b,sizeof(a)) #define REP(i, begin, m) for (int i = begin; i < begin+m; i ++) using namespace std; typedef long long LL; typedef vector <int> VI; typedef set <int> SETI; typedef queue <int> QI; typedef stack <int> SI; const int oo = 0x7fffffff; VI num; LL dp[20][20][4000]; LL dfs(int pos, int o, int left_right, bool limit){ if (pos == -1) return (left_right == 2000); if (!limit && ~dp[pos][o][left_right]) return dp[pos][o][left_right]; int end = limit?num[pos] : 9; LL res = 0; for (int i = 0; i <= end; i ++){ res += dfs(pos-1, o, left_right+i*(o-pos), limit && (i==end)); } if (!limit) dp[pos][o][left_right] = res; return res; } LL cal(LL x){ if (x < 0) return 0; num.clear(); while(x){ num.push_back(x%10); x /= 10; } MEM(dp, -1); LL ans = 0; for (int i = 0; i < (int)num.size(); i ++){ ans += dfs(num.size()-1, i, 2000, 1); } return ans - num.size() + 1; } int main(){ //freopen("test.in", "r", stdin); //freopen("test.out", "w", stdout); int t; scanf("%d", &t); while(t --){ LL x, y; scanf("%I64d %I64d", &x, &y); printf("%I64d\n", cal(y) - cal(x-1)); } return 0; } [/cpp]

轉(zhuǎn)載于:https://www.cnblogs.com/AbandonZHANG/p/4114313.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的HDU 3709 Balanced Number (数位DP)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。