2015 whu school competition f question big data (dp + Tips), 2015whu
Given five numbers of n (n <= 100), a, B, l, and r have another function sequence f (x), where f (x + 1) = f (x) + a or f (x) + B, f (0) = 0, ask how many such function sequences f (1) to f (n) so that the sum of the function sequence is between l and r.
The solution is as follows:
There is an error in the image. The result is a * (n + 1) * n instead of (B-a) * (n + 1) * n. In addition, pay attention to the problem of rounded up and rounded down in x/c.
This question has been suspended for a month. Today, I have finally found the time for ac. It's a little touching.
The Code is as follows:
# Include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <iostream> # include <algorithm> # include <vector> # include <map> # include <queue> # include <stack> # include <string> # include <map> # include <set> using namespace std; # define LL long const LL maxn = 100 + 5; const ll inf = 0x3f3f3f; LL d [(maxn + 1) * maxn/2]; // freopen ("input.txt", "r", stdin); void solve (int n) {memset (d, 0, Sizeof (d); d [0] = 1; for (int I = 1; I <= n; I ++) {for (int j = (I + 1) * I/2; j> = I; j --) if (d [j] + d [j-I]> 1000000000000000000) d [j] = (d [j] + d [j-I]) % 1000000007; else d [j] = d [j] + d [j-I] ;}} int main () {int n, a, B, L, R; while (scanf ("% d", & n) = 1) {scanf ("% d", & a, & B, & L, & R); if (B! = A) {if (B <a) swap (a, B); float xl = ceil (float) (L-a * n * (n + 1)/2) /(float) (B-a), xr = floor (float) (R-a * n * (n + 1)/2)/(float) (B-a); // The ceil function is rounded up, and the floor function is rounded down to solve (n); LL ans = 0; if (xl <0) xl = 0; if (xr> n * (n + 1)/2) xr = n * (n + 1)/2; for (int I = xl; I <= xr; I ++) if (ans + d [I]> 1000000000000000000) ans = (ans + d [I]) % 1000000007; else ans + = d [I]; // for (int I = xl; I <= xr; I ++) printf ("% lld \ n", d [I]); printf ("% d \ n", ans % 1000000007); // printf ("% lld", d [0]);} else {int xl = (L-a * n * (n + 1)/2), xr = (R-a * n * (n + 1)/2 ); LL ans = 1; if (0 <= xr & 0> = xl) {for (int I = 1; I <= n; I ++) ans = (ans * 2) % 1000000007; printf ("% d \ n", ans);} else printf ("0 \ n") ;}} return 0 ;}