1. 程式人生 > >bzoj1002 [FJOI2007]輪狀病毒 矩陣樹定理

bzoj1002 [FJOI2007]輪狀病毒 矩陣樹定理

Description


在這裡插入圖片描述
在這裡插入圖片描述
求這樣一個東西的生成樹方案數量

Solution


正解可能是dp啥的,直接上矩陣樹定理然後套高精度就完事兒了
如果把矩陣寫出來可以發現對角線上全是3,然後兩邊都是-1。找一波規律可以發現f[n]=3f[n-1]-f[n-2]+2

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define fill(x,t) memset(x,t,sizeof(x)) const int MOD=1000; const int N=105; const int L=105; struct num { int s[L],len; inline bool operator ==(num b) { num a=*this; if (a.len!=b.len) return 0; drp(i,a.len,1) if (a.s[i]!=b.s[i]) return 0; return 1; } inline bool operator !=(num b) { return
!(*this==b); } inline bool operator <(num b) { num a=*this; if (a.len<b.len) return 1; if (a.len>b.len) return 0; drp(i,a.len,1) { if (a.s[i]<b.s[i]) return 1; else if (a.s[i]>b.s[i]) return 0; } return 0; } inline bool operator <=(num b) { num a=*this; if (
a<b||a==b) return 1; return 0; } inline bool operator >(num b) { num a=*this; if (a.len>b.len) return 1; if (a.len<b.len) return 0; drp(i,a.len,1) { if (a.s[i]>b.s[i]) return 1; else if (a.s[i]<b.s[i]) return 0; } return 0; } inline bool operator >=(num b) { num a=*this; if (a>b||a==b) return 1; return 0; } inline num operator +(num b) { num a=*this,c=(num) { {0},std:: max(a.len,b.len)}; int v=0; rep(i,1,c.len) { c.s[i]=(a.s[i]+b.s[i]+v)%MOD; v=(a.s[i]+b.s[i]+v)/MOD; } if (v) { c.len+=1; c.s[c.len]=v; } return c; } inline num operator -(num b) { num a=*this,c=(num) { {0},std:: max(a.len,b.len)}; rep(i,1,c.len) { c.s[i]=a.s[i]-b.s[i]; if (c.s[i]<0) { c.s[i]+=MOD; a.s[i+1]-=1; } } while (!c.s[c.len]&&c.len>1) c.len-=1; return c; } inline num operator *(num b) { num a=*this,c=(num) { {0},a.len+b.len}; rep(i,1,a.len) { rep(j,1,b.len) { c.s[i+j-1]+=a.s[i]*b.s[j]; } } rep(i,1,a.len+b.len) { c.s[i+1]+=c.s[i]/MOD; c.s[i]%=MOD; } while (!c.s[c.len]&&c.len>1) c.len-=1; return c; } inline num operator /(int b) { num a=*this,c=(num) { {0},a.len}; int v=0; drp(i,len,1) { int t=v*MOD+a.s[i]; c.s[i]=t/b; v=t%b; } while (!c.s[c.len]&&c.len>1) c.len-=1; return c; } inline void read() { fill(s,0); len=0; char st[L]; scanf("%s",st); int v=0,i; for (i=strlen(st)-1; i >= 3; i-=3) { rep(j,i-2,i) v=v*10+st[j]-'0'; s[++len]=v; v=0; } rep(j,0,i) v=v*10+st[j]-'0'; s[++len]=v; } inline void output() { num tmp=*this; int i=tmp.len; while (!tmp.s[i]&&i>1) { i-=1; } printf("%d",tmp.s[i]); drp(j,i-1,1) { int v=tmp.s[j]; int f[5]= {0}; rep(k,1,3) { f[k]=v%10; v/=10; } drp(k,3,1) printf("%d",f[k]); } printf("\n"); } } f[N],two,thr; int main(void) { int n; scanf("%d",&n); two.len=1; two.s[1]=2; thr.len=1; thr.s[1]=3; f[1].len=f[1].s[1]=f[2].len=1; f[2].s[1]=5; rep(i,3,n) f[i]=f[i-1]*thr-f[i-2]+two; f[n].output(); return 0; }