1. 程式人生 > >233 Matrix題解

233 Matrix題解

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1355    Accepted Submission(s): 806


Problem Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a 0,1
 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,m in the 233 matrix?  
Input There are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9
). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).  
Output For each case, output a n,m mod 10000007.  
Sample Input

  
   1 112 20 03 723 47 16
  
 
 
   
 
 
Sample Output

  
   234279972937
   
    
     Hint
 
Source 2014 ACM/ICPC Asia Regional Xi'an Online

思路:

將第一列改寫成如下:

23

a1

a2

a3

a4

3(這個3是為了計算下一列而存在的)

然後計算出第二列:

23*10+3

23*10+3+a1

23*10+3+a1+a2

23*10+3+a1+a2+a3

23*10+3+a1+a2+a3+a4

3(為了繼續計算下一列保留這個3)

然後計算第三列……

受以上的啟發推匯出公式:


最後,目測測試資料很大,應該是用快速冪矩陣。

AC C++:

#include <cstdio>      
#include <algorithm> 
#define MOD 10000007
#define INF 0x3f3f3f3f  
#define N 10005  
//#define DEBUG
typedef long long LL; 
  
struct Matrix  
{  
    LL m[15][15];  
};  

int n; 
LL m;
LL a[20]; 
Matrix I; 

  
Matrix mult(Matrix a,Matrix b)  
{  
    Matrix c;  
    for(int i=0;i<n+2;i++)  
        for(int j=0;j<n+2;j++)  
        {  
             c.m[i][j]=0;  
             for(int k=0;k<n+2;k++)  
                 c.m[i][j]+=(a.m[i][k]*b.m[k][j]);  
             c.m[i][j]%=MOD;  
        }  
    return c;  
}  

Matrix quick_mod(Matrix a,LL n)  
{  
    Matrix c=I;  
    while(n)  
    {  
        if(n&1)  c=mult(c,a);  
        n>>=1;  
        a=mult(a,a);  
    }  
    return c;  
}  
  
int main()  
{  
    while(~scanf("%d%lld",&n,&m))  
    {  
        a[0]=3;  
        a[1]=23;  
        for(int i=2;i<n+2;i++)  
            scanf("%lld",&a[i]); 
			 
        for(int i=0;i<n+2;i++)  
            for(int j=0;j<n+2;j++)  
                if(i==j)  
                  I.m[i][j]=1;  
                else  
                  I.m[i][j]=0;
				    
#ifdef DEBUG
        for(int i=0;i<n+2;i++) 
        { 
            for(int j=0;j<n+2;j++) 
               printf("%lld ",I.m[i][j]); 
            printf("\n"); 
        }
#endif
		  
        Matrix A;  
        for(int i=0;i<n+2;i++)  
            for(int j=0;j<n+2;j++)  
                 if(i>=j)  
                    A.m[i][j]=1;  
                 else  
                    A.m[i][j]=0;  
        for(int i=1;i<n+2;i++)  
            A.m[i][1]=10;  
        LL ans=0;  
        Matrix B=quick_mod(A,m);
		  
#ifdef DEBUG
       for(int i=0;i<n+2;i++) 
        { 
            for(int j=0;j<n+2;j++) 
               printf("%lld ",B.m[i][j]); 
            printf("\n"); 
        }
#endif
		  
        for(int i=0;i<n+2;i++)  
            ans=(ans+B.m[n+1][i]*a[i]%MOD)%MOD;  
        printf("%lld\n",ans);  
    }  
    return 0;  
}


引用出處:

http://blog.csdn.net/ydd97/article/details/47684463

http://blog.csdn.net/lvshubao1314/article/details/39288393