hdu 5106 Bits Problem(數位dp),hdu5106

來源:互聯網
上載者:User

hdu 5106 Bits Problem(數位dp),hdu5106

題目連結:hdu 5106 Bits Problem

題目大意:給定n和r,要求算出[0,r)之間所有n-onebit數的和。

解題思路:數位dp,一個ct表示個數,dp表示和,然後就剩下普通的數位dp了。不過貌似正解是o(n)的演算法,但是n才

1000,用o(n^2)的複雜度也是夠的。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int mod = 1000000007;const int maxn = 1005;int n;char s[maxn];ll bit[maxn], dp[maxn][maxn], ct[maxn][maxn];int solve () {    memset(ct, 0, sizeof(ct));    memset(dp, 0, sizeof(dp));    int l = strlen(s), c = n;    ll sum = 0;    for (int i = 0; i < l; i++) {        for (int k = 0; k <= n; k++) {            if (ct[i][k] == 0)                continue;            for (int j = 0; j < 2; j++) {                if (j && k == 0)                    continue;                ct[i+1][k-j] = (ct[i+1][k-j] + ct[i][k]) % mod;                dp[i+1][k-j] = (dp[i+1][k-j] + dp[i][k] + bit[l-i-1] * j * ct[i][k]) % mod;            }        }        for (int j = 0; j < s[i]-'0'; j++) {            ct[i+1][c-j] = (ct[i+1][c-j] + 1) % mod;            dp[i+1][c-j] = (dp[i+1][c-j] + sum + bit[l-i-1] * j) % mod;        }        if (s[i] == '1') {            sum = (sum + bit[l-i-1]) % mod;            c--;        }    }    return dp[l][0];}int main () {    bit[0] = 1;    for (int i = 1; i <= 1000; i++)        bit[i] = bit[i-1] * 2 % mod;    while (scanf("%d%s", &n, s) == 2) {        printf("%d\n", solve());    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.