HDU 3709 Balanced Number (数位DP)
生活随笔
收集整理的這篇文章主要介紹了
HDU 3709 Balanced Number (数位DP)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題意
求出[x, y] 范圍內(nèi)的平衡數(shù),平衡數(shù)定義為:以數(shù)中某個位為軸心,兩邊的數(shù)的偏移量為矩,數(shù)位權(quán)重,使得整個數(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ù)的個數(shù)。 注意:1.減法可能為負(fù),所以需要加一個位移偏量,這里我設(shè)為2000;2.對于一個正數(shù),如果它是 Balanced Number,那么它有且只有一個平衡點(diǎn)。但是計(jì)算0時枚舉所有的支點(diǎn)都會把他算在內(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ān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的HDU 3709 Balanced Number (数位DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快易花一般几点抢额度?快易花抢额度时间介
- 下一篇: BZOJ 1026 windy数 (数位