传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1002
这种题真是有毒,很多叼一点的都用matrix tree定理推出了递推公式,也有一些用好几维递推也可以解,然而我一个都看不懂。总结:下次遇到这种题就直接打表找规律,这是坠吼滴!
#include <cstdio> #include <cstring> const int maxn = 105, mod = 10000; int n; struct gao { int data[100], wei; gao(void) { memset(data, 0, sizeof data); wei = 0; } gao operator+(int aa) const { gao rt; rt.data[0] = aa; for (int i = 0; i < wei; ++i) { rt.data[i] += data[i]; if (rt.data[i] >= mod) { rt.data[i] -= mod; ++rt.data[i + 1]; } } rt.wei = wei + (rt.data[wei]? 1: 0); return rt; } gao operator-(const gao & rhs) const { gao rt; for (int i = 0; i < wei; ++i) { rt.data[i] += data[i] - rhs.data[i]; if (rt.data[i] < 0) { rt.data[i] += mod; --rt.data[i + 1]; } } rt.wei = wei; while (rt.wei > 0 && !rt.data[rt.wei - 1]) { --rt.wei; } return rt; } gao operator*(int aa) const { gao rt; for (int i = 0; i < wei; ++i) { rt.data[i] += data[i] * aa; rt.data[i + 1] += rt.data[i] / mod; rt.data[i] %= mod; } rt.wei = wei + (rt.data[wei]? 1: 0); return rt; } void print(void) { printf("%d", data[wei - 1]); for (int i = wei - 2; i >= 0; --i) { printf("%04d", data[i]); } } } f[maxn]; int main(void) { f[1].data[0] = 1; f[1].wei = 1; f[2].data[0] = 5; f[2].wei = 1; scanf("%d", &n); for (int i = 3; i <= n; ++i) { f[i] = f[i - 1] * 3 - f[i - 2] + 2; } f[n].print(); puts(""); return 0; }