@OceanEye4年前
11/26
00:50
矩阵树定理裸题
矩阵树定理的传送门
写了个快速幂版本的,还没测试过可以跑多少
如果是取模快速幂的话应该n=1e8是稳的
虽然我知道可以找规律
但是还是用矩阵树定理整出了基尔霍夫矩阵之后再算行列式推公式
以后就可以出一个BZOJ1002加强版233
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
#include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <bits/stdc++.h> #define ll long long #define INF 1000000000 #define p(x) ('0'<=x&&x<='9') #define For(i,a,b) for(int i=a;i<=b;++i) #define Rev(i,a,b) for(int i=a;i>=b;--i) char cc; int C; template <class T> void read( T &x ) { x=0; cc=getchar(); C=1; while(!p(cc)) { if(cc=='-') C=-1; cc=getchar(); } while(p(cc)) { x=x*10+cc-48; cc=getchar(); } x*=C; } #undef p #define pb push_back #define pq priority_queue using namespace std; struct _high_precision_ { #define M 100 #define u_bound 1000000 ll Arr[M]; int top,p; _high_precision_() { memset(Arr,0,sizeof(Arr)); top=0; p=1; } void init(int x) { Arr[0]=x; top=0; } void do_carry() { int node,last; for(last=99;~last&&!Arr[last];--last); node=0; while(node<=last) { if(abs(Arr[node])>u_bound) { Arr[node+1]+=Arr[node]/u_bound; Arr[node]%=u_bound; } ++node; } if(Arr[node]) top=node; else top=!node?node:node-1; } void do_down() { while(!Arr[top]&&top>0) --top; for(int i=top;i;--i) if(Arr[i]>0) { Arr[i-1]+=u_bound; Arr[i]-=1; Arr[i]+=Arr[i-1]/u_bound; Arr[i-1]%=u_bound; } else { Arr[i-1]-=u_bound; Arr[i]+=1; Arr[i]+=Arr[i-1]/u_bound; Arr[i-1]%=u_bound; } } _high_precision_ operator *(const _high_precision_ &x) { _high_precision_ ret; ret.p=p*x.p; For(i,0,top) For(j,0,x.top) ret.Arr[i+j]+=Arr[i]*x.Arr[j]; ret.do_carry(); return ret; } void operator +=(const _high_precision_ &x) { int _top=max(top,x.top); _high_precision_ ret; For(i,0,_top) ret.Arr[i]=Arr[i]+x.Arr[i]; ret.do_carry(); ret.do_down(); if(!ret.Arr[ret.top]) --ret.top; memcpy(Arr,ret.Arr,sizeof(Arr)); p=(ret.Arr[ret.top]>=0?1:-1); top=ret.top; } _high_precision_ operator +(const _high_precision_ &x) { int _top=max(top,x.top); _high_precision_ ret; For(i,0,_top) ret.Arr[i]=Arr[i]+x.Arr[i]; ret.do_carry(); ret.do_down(); if(!ret.Arr[ret.top]) --ret.top; ret.p=(ret.Arr[ret.top]>=0?1:-1); return ret; } void operator =(const int & x) { if(x<0) p=-1; else p=1; top=0; Arr[0]=x; do_carry(); } void operator =(const ll& x) { if(x<0) p=-1; else p=1; top=0; Arr[0]=x; do_carry(); } void print(char _end) { if(p==-1) putchar('-'); for(int i=top;~i;--i) printf("%lld",Arr[i]*p); putchar(_end); } #undef u_bound #undef M }; struct hpMatrix { int m,n; _high_precision_ A[5][5]; hpMatrix() { memset(A,0,sizeof(A)); m=n=0; } _high_precision_* operator [](const int &x) { return A[x]; } hpMatrix operator *(const hpMatrix &B) { hpMatrix ret; ret.m=m; ret.n=B.n; For(i,0,m-1) For(k,0,B.n-1) For(j,0,n-1) ret.A[i][k]+=A[i][j]*B.A[j][k]; return ret; } /* void operator *=(hpMatrix B) { hpMatrix ret; ret.m=m; ret.n=B.n; For(i,0,m-1) For(k,0,B.n-1) For(j,0,n-1) ret[i][k]+=A[i][j]*B.A[j][k]; n=ret.n; For(i,0,m-1) For(j,0,n-1) A[i][j]=ret[i][j]; } */ }; /* struct Matrix { int m,n; }; */ hpMatrix trans,res,res1; int n; void ksm(int n) { for(;n;n>>=1) { if(n&1) res=res*trans; trans=trans*trans; } } void solve(int n) { for(;n;--n) res1=res1*trans; } void init() { trans.m=trans.n=3; trans[0][0]=3; trans[0][1]=1; trans[1][0]=-1; trans[2][0]=2; trans[2][2]=1; res.m=1;res.n=3; res[0][0]=5; res[0][1]=1; res[0][2]=1; res1=res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(NULL)); #ifdef LOCALTEST freopen("input.txt", "r", stdin); #endif init(); read(n); if(n==1) { puts("1"); return 0; } if(n==2) { puts("5"); return 0; } n-=2; solve(n); ksm(n); res.A[0][0].print('\n'); res1.A[0][0].print('\n'); // printf("%d\n",res.A[0][0].p); return 0; } |