超级IO
#define OPENIOBUF
namespace FASTIO{
class FastIOBase{
protected:
#ifdef OPENIOBUF
static const int BUFSIZE=1<<16;
char buf[BUFSIZE+1];
int buf_p=0;
#endif
FILE* target;
FastIOBase(FILE* f):target(f){}
~FastIOBase()=default;
public:
#ifdef OPENIOBUF
virtual void flush() = 0;
#endif
};
class FastOutput final:public FastIOBase{
private:
void __putc(char x){
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE)flush();
#else
putc(x, target);
#endif
}
template<typename T>
void __write(T x){
char stk[std::numeric_limits<T>::digits10+1],*top=stk;
if(x<0)return __putc('-'),__write(-x);
do*(top++)=x%10,x/=10;while(x);
for(;top!=stk;__putc(*(--top)+'0'));
}
public:
FastOutput(FILE* f=stdout):FastIOBase(f){}
#ifdef OPENIOBUF
~FastOutput(){flush();}
void flush(){fwrite(buf,1,buf_p,target),buf_p=0;}
#endif
FastOutput& operator <<(char x){
return __putc(x),*this;
}
FastOutput& operator <<(const char* s){
for (;*s;__putc(*(s++)));return *this;
}
FastOutput& operator <<(const std::string& s){
return (*this)<<s.c_str();
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastOutput&>operator <<(const T& x){
return __write(x),*this;
}
template<typename ...T>
void writesp(const T &...x){
std::initializer_list<int>{(this->operator<<(x),__putc(' '),0)...};
}
template<typename ...T>
void writeln(const T &...x){
std::initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...};
}
template<typename Iter>
void writesp(Iter begin,Iter end){
while(begin!=end)(*this)<<*(begin++)<<' ';
}
template<typename Iter>
void writeln(Iter begin,Iter end){
while(begin!=end)(*this)<<*(begin++)<<'\n';
}
}cout;
class FastInput final:public FastIOBase{
private:
bool __eof;
public:
FastInput(FILE* f=stdin):FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{
buf_p=BUFSIZE;
}
void flush(){buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0;}
bool eof()const{return buf[buf_p]==EOF;}
#else
{}
bool eof()const{return feof(target);}
#endif
char get(){
if(__eof)return EOF;
#ifdef OPENIOBUF
if(buf_p==BUFSIZE)flush();
char ch=buf[buf_p++];
#else
char ch=getc(target);
#endif
return ~ch?ch:(__eof=true,EOF);
}
void unget(char c){
__eof=false;
#ifdef OPENIOBUF
buf[--buf_p]=c;
#else
ungetc(c,target);
#endif
}
explicit operator bool()const{return !__eof;}
FastInput& operator >>(char&x){
while(isspace(x=get()));return *this;
}
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastInput&>operator>>(T& x){
char ch,sym=0;x=0;
while(isspace(ch=get()));
if(__eof)return *this;
if(ch=='-')sym=1,ch=get();
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
return unget(ch),sym?x=-x:x,*this;
}
FastInput& operator >>(char* s){
while(isspace(*s=get()));
if(__eof)return *this;
for(;!isspace(*s) and !__eof;*(++s)=get());
return unget(*s),*s='\0',*this;
}
FastInput& operator >>(std::string&s){
char str_buf[(1<<8)+1]={0},*p=str_buf;
char* const buf_end=str_buf+(1<<8);
while(isspace(*p=get()));
if(__eof)return *this;
for(s.clear(),p++;;p=str_buf){
for(;p!= buf_end && !isspace(*p=get()) && !__eof;p++);
if(p!=buf_end)break;
s.append(str_buf);
}
unget(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &...x){
std::initializer_list<int>{(this->operator>>(x),0)...};
}
template<typename Iter>
void read(Iter begin,Iter end){
while(begin!=end)(*this)>>*(begin++);
}
}cin;
}
//using namespace FASTIO;
BigInt
#define BigInteger bigint
class BigInteger
{
private:
static const int BASE = 100000000; static const int WIDTH = 8; bool sign; size_t length; std::vector<int>num; void cutLeadingZero(); void setLength();
public:
BigInteger(int n = 0); BigInteger(long long n); BigInteger(const char* n); BigInteger(const BigInteger& n); long long toint(); const BigInteger& operator=(int n); const BigInteger& operator=(long long n); const BigInteger& operator=(const char* n); const BigInteger& operator=(const BigInteger& n); size_t size()const; BigInteger e(size_t n)const; BigInteger abs()const; const BigInteger& operator+()const; friend BigInteger operator+(const BigInteger& a, const BigInteger& b); const BigInteger& operator+=(const BigInteger& n); const BigInteger& operator++(); BigInteger operator++(int); BigInteger operator-()const; friend BigInteger operator-(const BigInteger& a, const BigInteger& b);
const BigInteger& operator-=(const BigInteger& n); const BigInteger& operator--(); BigInteger operator--(int); friend BigInteger operator*(const BigInteger& a, const BigInteger& b); const BigInteger& operator*=(const BigInteger& n); friend BigInteger operator/(const BigInteger& a, const BigInteger& b); const BigInteger& operator/=(const BigInteger& n); friend BigInteger operator%(const BigInteger& a, const BigInteger& b); const BigInteger& operator%=(const BigInteger& n);
friend bool operator<(const BigInteger& a, const BigInteger& b); friend bool operator<=(const BigInteger& a, const BigInteger& b); friend bool operator>(const BigInteger& a, const BigInteger& b); friend bool operator>=(const BigInteger& a, const BigInteger& b); friend bool operator==(const BigInteger& a, const BigInteger& b); friend bool operator!=(const BigInteger& a, const BigInteger& b); friend bool operator||(const BigInteger& a, const BigInteger& b); friend bool operator&&(const BigInteger& a, const BigInteger& b); bool operator!(); friend std::ostream& operator<< (std::ostream& out, const BigInteger& n); friend std::istream& operator>>(std::istream& in, BigInteger& n);
};
inline void BigInteger::cutLeadingZero() { while(num.back() == 0 && num.size() != 1) { num.pop_back(); } }inline void BigInteger::setLength() { cutLeadingZero(); int tmp = num.back(); if (tmp == 0) { length = 1; } else { length = (num.size() - 1) * 8; while (tmp > 0) { ++length; tmp /= 10; } } }inline long long BigInteger::toint() { long long res = 0; for (int i = this->num.size(); i >= 0; i--) { res = res * 10 + num[i]; }return res; }
BigInteger::BigInteger(int n) { *this = n; }BigInteger::BigInteger(long long n) { *this = n; }BigInteger::BigInteger(const char* n) { *this = n; }BigInteger::BigInteger(const BigInteger& n) { *this = n; }const BigInteger& BigInteger::operator=(int n) { *this = (long long)n; return *this; }const BigInteger& BigInteger::operator=(long long n) { num.clear(); if (n == 0) { num.push_back(0); }if (n >= 0) { sign = true; } else if (n == LLONG_MIN) { *this = "9223372036854775808"; this->sign = false; return*this; } else if (n < 0) { sign = false; n = -n; }while (n != 0) { num.push_back(n % BASE); n /= BASE; }setLength(); return*this; }
const BigInteger& BigInteger::operator=(const char* n) { int len = strlen(n); int tmp = 0; int ten = 1; int stop = 0; num.clear(); sign = (n[0] != '-'); if (!sign) { stop = 1; }for (int i = len; i > stop; --i) { tmp += (n[i - 1] - '0') * ten; ten *= 10; if ((len - i) % 8 == 7) { num.push_back(tmp); tmp = 0; ten = 1; } }if ((len - stop) % WIDTH != 0) { num.push_back(tmp); }setLength(); return*this; }
const BigInteger& BigInteger::operator=(const BigInteger& n) { num = n.num; sign = n.sign; length = n.length; return*this; }size_t BigInteger::size()const { return length; }BigInteger BigInteger::e(size_t n)const { int tmp = n % 8; BigInteger ans; ans.length = n + 1; n /= 8; while (ans.num.size() <= n) { ans.num.push_back(0); }ans.num[n] = 1; while (tmp--) { ans.num[n] *= 10; }return ans * (*this); }BigInteger BigInteger::abs()const { BigInteger ans(*this); ans.sign = true; return ans; }const BigInteger& BigInteger::operator+()const { return*this; }
BigInteger operator+(const BigInteger& a, const BigInteger& b) { if (!b.sign) { return a - (-b); }if (!a.sign) { return b - (-a); }BigInteger ans; int carry = 0; int aa, bb; size_t lena = a.num.size(); size_t lenb = b.num.size(); size_t len = max(lena, lenb); ans.num.clear(); for (size_t i = 0; i < len; ++i) { if (lena <= i) { aa = 0; } else { aa = a.num[i]; }if (lenb <= i) { bb = 0; } else { bb = b.num[i]; }ans.num.push_back((aa + bb + carry) % BigInteger::BASE); carry = (aa + bb + carry) / BigInteger::BASE; }if (carry > 0) { ans.num.push_back(carry); }ans.setLength(); return ans; }
const BigInteger& BigInteger::operator+=(const BigInteger& n) { *this = *this + n; return*this; }const BigInteger& BigInteger::operator++() { *this = *this + 1; return*this; }BigInteger BigInteger::operator++(int) { BigInteger ans(*this); *this = *this + 1; return ans; }BigInteger BigInteger::operator-()const { BigInteger ans(*this); if (ans != 0) { ans.sign = !ans.sign; }return ans; }
BigInteger operator-(const BigInteger& a, const BigInteger& b) { if (!b.sign) { return a + (-b); }if (!a.sign) { return-((-a) + b); }if (a < b) { return-(b - a); }BigInteger ans; int carry = 0; int aa, bb; size_t lena = a.num.size(); size_t lenb = b.num.size(); size_t len = max(lena, lenb); ans.num.clear(); for (size_t i = 0; i < len; ++i) { aa = a.num[i]; if (i >= lenb) { bb = 0; } else { bb = b.num[i]; }ans.num.push_back((aa - bb - carry + BigInteger::BASE) % BigInteger::BASE); if (aa < bb + carry) { carry = 1; } else { carry = 0; } }ans.setLength(); return ans; }const BigInteger& BigInteger::operator-=(const BigInteger& n) { *this = *this - n; return*this; }const BigInteger& BigInteger::operator--() { *this = *this - 1; return*this; }BigInteger BigInteger::operator--(int) { BigInteger ans(*this); *this = *this - 1; return ans; }
BigInteger operator*(const BigInteger& a, const BigInteger& b) { size_t lena = a.num.size(); size_t lenb = b.num.size(); std::vector<long long>ansLL; for (size_t i = 0; i < lena; ++i) { for (size_t j = 0; j < lenb; ++j) { if (i + j >= ansLL.size()) { ansLL.push_back((long long)a.num[i] * (long long)b.num[j]); } else { ansLL[i + j] += (long long)a.num[i] * (long long)b.num[j]; } } }while (ansLL.back() == 0 && ansLL.size() != 1) { ansLL.pop_back(); }size_t len = ansLL.size(); long long carry = 0; long long tmp; BigInteger ans; ans.sign = (ansLL.size() == 1 && ansLL[0] == 0) || (a.sign == b.sign); ans.num.clear(); for (size_t i = 0; i < len; ++i) { tmp = ansLL[i]; ans.num.push_back((tmp + carry) % BigInteger::BASE); carry = (tmp + carry) / BigInteger::BASE; }if (carry > 0) { ans.num.push_back(carry); }ans.setLength(); return ans; }const BigInteger& BigInteger::operator*=(const BigInteger& n) { *this = *this * n; return*this; }
BigInteger operator/(const BigInteger& a, const BigInteger& b) { BigInteger aa(a.abs()); BigInteger bb(b.abs()); if (aa < bb) { return 0; }char* str = new char[aa.size() + 1]; memset(str, 0, sizeof(char) * (aa.size() + 1)); BigInteger tmp; int lena = aa.length; int lenb = bb.length; for (int i = 0; i <= lena - lenb; ++i) { tmp = bb.e(lena - lenb - i); while (aa >= tmp) { ++str[i]; aa = aa - tmp; }str[i] += '0'; }BigInteger ans(str); delete[]str; ans.sign = (ans == 0 || a.sign == b.sign); return ans; }const BigInteger& BigInteger::operator/=(const BigInteger& n) { *this = *this / n; return*this; }BigInteger operator%(const BigInteger& a, const BigInteger& b) { return a - a / b * b; }const BigInteger& BigInteger::operator%=(const BigInteger& n) { *this = *this - *this / n * n; return*this; }
bool operator<(const BigInteger& a, const BigInteger& b) { if (a.sign && !b.sign) { return false; } else if (!a.sign && b.sign) { return true; } else if (a.sign && b.sign) { if (a.length < b.length) { return true; } else if (a.length > b.length) { return false; } else { size_t lena = a.num.size(); for (int i = lena - 1; i >= 0; --i) { if (a.num[i] < b.num[i]) { return true; } else if (a.num[i] > b.num[i]) { return false; } }return false; } } else { return-b < -a; } }
bool operator<=(const BigInteger& a, const BigInteger& b) { return!(b < a); }bool operator>(const BigInteger& a, const BigInteger& b) { return b < a; }bool operator>=(const BigInteger& a, const BigInteger& b) { return!(a < b); }bool operator==(const BigInteger& a, const BigInteger& b) { return!(a < b) && !(b < a); }bool operator!=(const BigInteger& a, const BigInteger& b) { return(a < b) || (b < a); }bool operator||(const BigInteger& a, const BigInteger& b) { return a != 0 || b != 0; }bool operator&&(const BigInteger& a, const BigInteger& b) { return a != 0 && b != 0; }
bool BigInteger::operator!() { return*this == 0; }std::ostream& operator<<(std::ostream& out, const BigInteger& n) { size_t len = n.num.size(); if (!n.sign) { out << '-'; }out << n.num.back(); for (int i = len - 2; i >= 0; --i) { out << std::setw(BigInteger::WIDTH) << std::setfill('0') << n.num[i]; }return out; }
std::istream& operator>> (std::istream& in, BigInteger& n) { std::string str; in >> str; size_t len = str.length(); size_t i, start = 0; if (str[0] == '-') { start = 1; }if (str[start] == '\0') { return in; }for (i = start; i < len; ++i) { if (str[i] < '0' || str[i]>'9') { return in; } }n = str.c_str(); return in; }
常用函数
#define ll long long
const int mod=1e9+7;
const int N=1005;
int max(int a,int b){return (a>b)?a:b;}
int min(int a,int b){return (a<b)?a:b;}
int gcd(int a,int b){return b>0?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
bool isprime(int x){if(x<=1)return 0;for(int i=2;i*i<=x;i++)if(x%i==0)return 0;return 1;}
ll qpow(ll a,ll b){ll res=1;while(b>0){if(b%2==1)res=(res*a)%mod;a=(a*a)%mod,b/=2;}return res;}
矩阵快速幂
#include<iostream>
#include<cstring>
#define mod 1000000007
#define ll long long
#define N 105
ll n,k;
struct Matrix{
ll matrix[N][N];
Matrix(){memset(matrix,0,sizeof(matrix));}
void buildMatrix(){for(int i=1;i<=n;i++)matrix[i][i]=1;}
}matrixA;
Matrix operator * (const Matrix &A,const Matrix &B){
Matrix C;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
C.matrix[i][j]=(C.matrix[i][j]+A.matrix[i][k]*B.matrix[k][j]%mod)%mod;
}
}
}
return C;
}
int main(){
std::ios::sync_with_stdio(0);
std::cin>>n>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
std::cin>>matrixA.matrix[i][j];
}
}
Matrix result;
result.buildMatrix();
do{
if(k&1)result=result*matrixA;
matrixA=matrixA*matrixA;k>>=1;
}while(k);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
std::cout<<result.matrix[i][j]<<' ';
}
std::cout<<std::endl;
}
}