【LOJ#6271】【长乐集训 2017 Day10】生成树求和 加强版(矩阵树定理)
由于是三进制不进位加法
考虑矩阵树求得是边权积的和
于是用做把加法转成乘法
做矩阵树之后再回来即可
至于逆元
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void Mul(int &a,int b){a=1ll*a*b%mod;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
cs int inv3=Inv(3);
struct plx{
int x,y;
plx(int _x=0,int _y=0):x(_x),y(_y){}
operator bool(){return !!(x+y);}
friend inline plx operator +(cs plx &a,cs plx &b){
return plx(add(a.x,b.x),add(a.y,b.y));
}
friend inline plx operator -(cs plx &a,cs plx &b){
return plx(dec(a.x,b.x),dec(a.y,b.y));
}
friend inline plx operator *(cs plx &a,cs plx &b){
return plx(((1ll*a.x*b.x-1ll*b.y*a.y)%mod+mod)%mod,((1ll*a.y*b.x+1ll*a.x*b.y-1ll*a.y*b.y)%mod+mod)%mod);
}
friend inline plx operator *(cs plx &a,cs int &b){
return plx(mul(a.x,b),mul(a.y,b));
}
inline void operator +=(cs plx &a){Add(x,a.x),Add(y,a.y);}
inline void operator -=(cs plx &a){Dec(x,a.x),Dec(y,a.y);}
inline void operator *=(cs plx &a){*this=*this*a;}
};
inline plx pInv(plx a){
return plx(dec(a.x,a.y),dec(0,a.y))*Inv(((1ll*a.x*a.x+1ll*a.y*a.y-1ll*a.x*a.y)%mod+mod)%mod);
}
cs plx w0=plx(1,0),w1=plx(0,1),w2=plx(mod-1,mod-1);
cs int N=105,M=10005;
struct edge{
int u,v,w;
}e[M];
int n,mx,v[M],res,m;
plx a[N][N],trans[3][3],ans[3];
inline plx calc(int n){
plx ans(1,0);
for(int i=1;i<=n;i++){
int pos;
for(pos=i;pos<=n;pos++)if(a[pos][i])break;
if(pos>n){ans=plx(0,0);break;}
if(pos!=i)swap(a[pos],a[i]),ans=plx(0,0)-ans;
plx iv=pInv(a[i][i]);
for(int j=i+1;j<=n;j++)if(a[j][i]){
plx t=iv*a[j][i];
for(int k=i;k<=n;k++)a[j][k]-=a[i][k]*t;
}
ans*=a[i][i];
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)a[i][j]=plx(0,0);
return ans;
}
int main(){
freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);
trans[0][0]=trans[1][0]=trans[2][0]=trans[0][1]=trans[0][2]=w0;
trans[1][1]=trans[2][2]=w1,trans[1][2]=trans[2][1]=w2;
n=read(),m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
e[i].u=u,e[i].v=v,e[i].w=w;
chemx(mx,e[i].w);
}
for(int p=1;p<=mx;p*=3){
for(int i=1;i<=m;i++)v[i]=(e[i].w/p)%3;
for(int c=0;c<=2;c++){
for(int i=1;i<=m;i++){
plx now=trans[c][v[i]];
int u=e[i].u,v=e[i].v;
a[u][u]+=now,a[v][v]+=now;
a[u][v]-=now,a[v][u]-=now;
}
ans[c]=calc(n-1);
}
plx now=ans[0]*3+ans[1]*w2+ans[2]*w1+(ans[1]*w1+ans[2]*w2)*2;
Add(res,mul(now.x,mul(p,inv3)));
}
cout<<res<<'\n';
}