POJ 3252 数位DP
生活随笔
收集整理的這篇文章主要介紹了
POJ 3252 数位DP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:
http://poj.org/problem?id=3252
題意:
給你一個區間l,r,求區間中有多少個數轉化為二進制后1的個數大于等于0的個數
題解:
還是數位dp,不過多了前導0的判斷
代碼:
31 int a[40]; 32 int dp[40][80]; 33 34 int dfs(int pos, int sta, bool lead, bool limit) { 35 if (pos == -1) return sta >= 40; 36 if (!lead && !limit && dp[pos][sta] != -1) return dp[pos][sta]; 37 int up = limit ? a[pos] : 1; 38 int res = 0; 39 rep(i, 0, up + 1) { 40 if (lead && i == 0) res += dfs(pos - 1, sta, true, limit && i == a[pos]); 41 else res += dfs(pos - 1, sta + (i == 0 ? 1 : -1), false, limit && i == a[pos]); 42 } 43 if (!lead && !limit) dp[pos][sta] = res; 44 return res; 45 } 46 47 int solve(int x) { 48 int pos = 0; 49 while (x) { 50 a[pos++] = x & 1; 51 x >>= 1; 52 } 53 return dfs(pos - 1, 40, true, true); 54 } 55 56 int main() { 57 int l, r; 58 cin >> l >> r; 59 memset(dp, -1, sizeof(dp)); 60 cout << solve(r) - solve(l - 1) << endl; 61 return 0; 62 }?
轉載于:https://www.cnblogs.com/baocong/p/6811341.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的POJ 3252 数位DP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python进程、线程、协程
- 下一篇: 基于MVVM的知乎日报应用安卓源码