2015 whu校賽f題big data(dp + 小技巧),2015whu

來源:互聯網
上載者:User

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;}

聯繫我們

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