#include #include #include #define MAX_DIMENSION 35 using namespace std; typedef __int64 MATRIX_TYPE; typedef __int64 MAX_INT_TYPE; typedef MATRIX_TYPE Matrix[MAX_DIMENSION][MAX_DIMENSION]; int ndim=MAX_DIMENSION; int mod=1234567891; void m_zero(Matrix x) { memset(x, 0, sizeof(MATRIX_TYPE)*MAX_DIMENSION*MAX_DIMENSION); } void m_one(Matrix x) { int i; m_zero(x); for(i=0;i>=1) { if ((n & 1) != 0) { m_multiple(x_p[k],y,temp); m_copy(temp,y); } } } void m_pow_sum1(Matrix x_p[],unsigned int n, Matrix y) { m_zero(y); if(n==0)return; if(n==1) m_copy(x_p[1],y); else { Matrix temp; m_pow_sum1(x_p,n>>1,temp); m_add(temp,y,y); Matrix temp2; m_pow_with_saved(x_p,n>>1,temp2); Matrix temp3; m_multiple(temp,temp2,temp3); m_add(temp3,y,y); if(n&1) { m_multiple(temp2,temp2,temp3); m_multiple(temp3,x_p[1],temp2); m_add(temp2,y,y); } } } void m_pow_sum(Matrix x, unsigned int n, Matrix y) { unsigned int i=0,logn,n2=n; for(logn=0,n2=n;n2;logn++,n2 >>= 1); Matrix *pow_arry=new Matrix[logn==0?2:(logn+1)]; m_one(pow_arry[0]); m_copy(x,pow_arry[1]); for(i=1;i