HDU5015 233 Matrix(矩阵快速幂)

    xiaoxiao2021-04-01  94

    题目:

    233 Matrix

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2180    Accepted Submission(s): 1274 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 1 1 2 2 0 0 3 7 23 47 16   Sample Output 234 2799 72937 Hint   Source 2014 ACM/ICPC Asia Regional Xi'an Online   Recommend hujie   |   We have carefully selected several similar problems for you:   6022  6021  6020  6019  6018    Statistic |  Submit |  Discuss |  Note 思路:

    这道题一看数据范围是10的九次方,那么普通方法肯定行不通,这时候我们用矩阵快速幂。

    关于构造矩阵:http://blog.csdn.net/u011721440/article/details/39401515

    代码:

    #include <cstdio> #include <cstring> #include <cctype> #include <string> #include <set> #include <iostream> #include <stack> #include <cmath> #include <queue> #include <vector> #include <algorithm> #define mem(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f #define mod 10000007 #define N 3 #define M 1000000+10 #define ll long long using namespace std; int n; struct Mat { ll mat[12][12]; void init() { memset(mat,0,sizeof(mat)); } void E() { init(); for(int i=0;i<n;i++) mat[i][i]=1; } void show() { printf("debug\n"); for(int i=0;i<=n+1;i++,puts("")) for(int j=0;j<=n+1;j++) printf("%lld ",mat[i][j]); printf("Over\n"); } }; Mat operator *(Mat a,Mat b) { Mat res; res.init(); for(int i=0;i<=n+1;i++) for(int j=0;j<=n+1;j++) for(int k=0;k<=n+1;k++) res.mat[i][j]+=a.mat[i][k]*b.mat[k][j],res.mat[i][j]%=mod; return res; } Mat ksm(Mat a,int b) { Mat res; res.E(); while(b>0) { if(b&1) res=a*res; a=a*a; b>>=1; } return res; } Mat getmat() { Mat res; res.init(); for(int i=0;i<n;i++) for(int j=0;j<=i;j++) res.mat[i][j]=1; for(int i=0;i<=n;i++) res.mat[n][i]=10; for(int i=0;i<=n+1;i++) res.mat[n+1][i]=1; return res; } int main() { int m; while(scanf("%d%d",&n,&m)!=EOF) { Mat ori; ori.init(); ori.mat[0][n]=23; ori.mat[0][n+1]=3; for(int i=n-1;i>=0;i--) scanf("%lld",&ori.mat[0][i]); Mat res=ori*ksm(getmat(),m); printf("%lld\n",res.mat[0][0]); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-665674.html

    最新回复(0)