WHU 1579 Big data (DP)

來源:互聯網
上載者:User

標籤:

題意:

      f[0]=0,f[i]=f[i-1]+a or b.

      求滿足L<=∑f[n]<=R的序列的種數

      n<100.  |a|,|b|<=10000.  |L|,|R|<1e9

 

 

Solution

      其實就是一個背包問題.

      當a=b,序列 0 a a+a和 0 b b+b 竟然算不同的序列= =,巨坑

 

#include <iostream>#define LL long longusing namespace std;const int MOD = (int) 1e9 + 7;const int M = 105;LL N, a, b, L, R;LL dp[109][109 * 109];int main() {    dp[0][0] = 1;    for (int i = 1; i <= M; i++)        for (int j = 0; j <= (i * (i - 1) / 2); j++) {            dp[i][j] = (dp[i - 1][j] + dp[i][j]) % MOD;            dp[i][j + i] = (dp[i - 1][j] + dp[i][j + i]) % MOD;        }    for (int i = 1; i <= M; i++)        for (int j =  (i + 1) * i / 2 - 1; j >= 0; j--)            dp[i][j] = (dp[i][j + 1] + dp[i][j]) % MOD;    while (cin >> N >> a >> b >> L >> R) {        if (a > b) swap (a, b);        LL s = a * (1 + N) * N / 2;        if (s <= R && b != a ) {            LL nl = (L - s ) / (b - a), nr = (R - s) / (b - a) + 1;            if ( (L - s) % (b - a) != 0) nl++;            if (L - s <= 0) nl = 0;            if (nl > (N + 1) *N / 2) nl = (N + 1) * N / 2  + 1;            if (nr > (N + 1) *N / 2) nr = (N + 1) * N / 2 + 1;            cout << (MOD + dp[N][nl] - dp[N][nr]) % MOD << endl;        }        else if (s >= L && s <= R && b == a) {            LL ans = 1;            for (int i = 1; i <= N; i++)                ans = (ans * 2) % MOD;            cout << ans << endl;        }        else            cout << 0 << endl;    }    return 0;}
View Code

 

WHU 1579 Big data (DP)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.