题意:很清楚 = =
思路:刚开始是二分+二分,就是求等比数列的前一半,然后前一半乘一个A^(k>>1)就是后一半的值,然后前后加起来,如果是奇数,那么再把这位加上,这样搞完了,过了,不过很慢。。然后百度了一下题解,发现另一种做法是构造一个矩阵B
B:A 1
0 1
然后发现B^(k+1):A(k+1) A(K)+A(K-1).....+A+1
0 1
于是直接一个快速幂就能求出来=。=(ps:1指的是单位矩阵
代码:
#include
#include
#include
#include
using namespace std;
const int N = 130;
//const int mod = 1e9+7;
#define ll long long
struct Matrix{
ll a[N][N];
Matrix(){
memset(a,0,sizeof(a));
for(int i = 0;i < N;i ++)
a[i][i] = 1;
}
};
int n,k;
ll mod;
Matrix multiply(Matrix a,Matrix b){
Matrix ans;
memset(ans.a,0,sizeof(ans.a));
for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++){
if(a.a[i][j]){
for(int k = 0;k < n;k ++){
ans.a[i][k] = (ans.a[i][k] + a.a[i][j] * b.a[j][k]%mod)%mod;
}
}
}
}
return ans;
}
Matrix add(Matrix a,Matrix b){
Matrix ans;
for(int i = 0;i < n;i ++)
for(int j = 0;j < n;j ++){
ans.a[i][j] = (a.a[i][j] + b.a[i][j])%mod;
}
return ans;
}
Matrix fast_mod(Matrix A,ll k){
Matrix ans;
while(k){
if(k&1)ans = multiply(ans,A);
A = multiply(A,A);
k = k>>1;
}
return ans;
}
int main(){
scanf("%d%d%lld",&n,&k,&mod);
Matrix mmb;
memset(mmb.a,0,sizeof(mmb.a));
for(int i = 0;i < n;i ++){
for(int j = 0;j < n;j ++)
scanf("%d",&mmb.a[i][j]);
}
for(int i = 0;i < n;i ++)mmb.a[i][i+n] = mmb.a[i+n][i+n] = 1;
n = n<<1;
mmb = fast_mod(mmb,k+1);
n = n>>1;
//mm = multiply(mm,mm);
for(int i = 0;i < n;i ++)mmb.a[i][i+n] --;
for(int i = 0;i < n;i ++){
for(int j = n;j < 2*n;j ++){
cout<<(mmb.a[i][j] + mod)%mod;
if(j != 2*n-1)cout<<' ';
}
cout<