标签:c++ logs == bit stp div pow while using
#include<bits/stdc++.h> using namespace std; const long long P=1e9+7; long long n,m; struct nob{ long long juzhen[105][105]; friend nob operator * (const nob &a,const nob &b){ nob c; for (int i=1; i<=n; i++){ for (int l=1; l<=n; l++){ long long sum=0; for (int k=1; k<=n; k++){ sum+=a.juzhen[i][k]*b.juzhen[k][l]; sum%=P; } c.juzhen[i][l]=sum; } } return c; } }s[100]; nob resite(){ nob a; for (int i=1; i<=n; i++){ for (int l=1; l<=n; l++){ if (i==l) a.juzhen[i][l]=1; else a.juzhen[i][l]=0; } } return a; } nob fastpow(nob x,long long k){ nob ans=resite(); while (k){ if (k&1) ans=x*ans; x=x*x; k>>=1; } return ans; } int main(){ cin>>n>>m; for (int i=1; i<=n; i++){ for (int l=1; l<=n; l++){ cin>>s[1].juzhen[i][l]; } } s[2]=fastpow(s[1],m); for (int i=1; i<=n; i++){ for (int l=1; l<=n; l++){ cout<<s[2].juzhen[i][l]<<" "; } cout<<endl; } return 0; }
感觉是一个极其丑陋而且比较慢的(应该吧,因为看起来比较慢)矩乘。QAQ
标签:c++ logs == bit stp div pow while using
原文地址:http://www.cnblogs.com/cain-/p/7560529.html