Boxes of Chocolates Again UVA - 10590

Boxes of Chocolates Again UVA - 10590

題目:https://odzkskevi.qnssl.com/7ee6f54041617f8f28cf81ea4c0484ec?v=1508356293

數的劃分。
描述狀態:f[i][j]:數i劃分數不超過j的種樹
狀態轉移方程:
f[i][j] = f[i][j-1]+f[i-j][j]
數i劃分數不超過j的種樹
=不劃分出j時數i劃分數不超過j-1的種樹+劃分出一個j後數i-j劃分數不超過j的種樹
存儲:5000*5000*高精度數組,空間不夠。考慮狀態轉移方程j只與j,j-1有關,所以可以狀態壓縮。此時將外層循環設爲j內層設爲i
初值:f[0][j] = 1;
高精度:考慮壓位加速

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 5001;
typedef long long LL;

struct NUM{
    LL a[200], tot;

    NUM() {
        memset(a, 0, sizeof a);
        tot = 1;
    }

    NUM operator + (const NUM& b) const{
        NUM c = b;
        for (int i = 1; i <= tot; ++i)
          c.a[i] += a[i];
        c.tot = max(tot, c.tot);
        for (int i = 1; i <= c.tot; ++i) {
          c.a[i+1] += c.a[i] / (LL)1e14;
          c.a[i] %= (LL)1e14;
        }
        if (c.a[c.tot+1]) c.tot++;
        return c;
    }

    void print(){
        printf("%lld", a[tot]);
        for (int i = tot-1; i >= 1; --i)
          printf("%014lld", a[i]);
        printf("\n");
    }
}f[MAXN][2], ans[MAXN];

int main()
{
    int n;
    ans[0].a[1] = 1;
    for (int j = 1; j <= 5000; ++j)
      for (int i = 1; i <= 5000; ++i) {
        if (i-j < 0) continue;
        if (i-j >= j)
          f[i][j%2] = f[i][(j-1)%2] + f[i-j][j%2];
        else
          f[i][j%2] = f[i][(j-1)%2] + ans[i-j];
        if (i == j)  ans[i] = f[i][i%2];
      }
    while (scanf("%d", &n) != EOF) {
      ans[n].print();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章