2015 whu校賽f題big data(dp + 小技巧),2015whu
題意是給定五個數n(n <= 100),a,b,l,r 另外有函數序列f(x),其中f(x + 1) = f(x) + a或f(x)+ b,f(0) = 0,問有多少個這樣的函數序列f(1)到f(n)使得函數序列的和在l和r之間
解題思路如下:
圖片有一處錯誤,要減去的是a*(n + 1) * n而不是 (b - a)* (n + 1) * n,此外,要注意x/c時向上取整和向下取整的問題。
這道題做做停停一個月了今天終於找時間ac了,有點感人呐
代碼如下:
#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 long const LL maxn = 100 + 5;const LL INF = 0x3f3f3f3f;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%d%d%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) ); //ceil函數向上取整,floor函數向下取整 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;}