A Simple Math Problem 矩阵乘法


A Simple Math Problem
Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

Input

The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.

Output

For each case, output f(k) % m in one line.

Sample Input

10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0

Sample Output

45
104

Author

linle

Source

2007省赛集训队练习赛(6)_linle专场
生气一开始,矩阵构造得不对,结果给出的数据过不了,真心恼火!!
后来仔细想想后,发觉可以构造出更好的矩阵,与是就过了 吐舌头
{f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8],f[9]}*A={f[10],f[11],f[12],f[13],f[14],f[15],f[16],f[17],f[18],f[19]}
只要构造出10*10矩阵A再用上矩阵快速幂就可以交上AC代码鸟~
~~AC不易,且行且珍惜
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define inf 9999999
const int MAXN = 10;
int M[MAXN][MAXN];
int f[MAXN];
int a[MAXN];
int m;
struct Matrix{
    int M[MAXN][MAXN];
}initm,unitm;
void initM(){//构造矩阵,这一步很重要
    int i, j,k;
    for (i = 0; i < 10; i++){
        for (j = 0; j < 10; j++)
            initm.M[i][j] = 0;
    }
    for (i = 0; i < 10; i++){
        for (j = 0; j < 10-i;j++)
            initm.M[9-j][i] = a[j+i];
    }
    for (i = 1; i < 10; i++){
        for (k = 0; k < i;k++)
            for (j = 0; j < 10; j++)
                initm.M[j][i] = (initm.M[j][i]+a[k]*initm.M[j][i - k-1]%m)%m;
    }
	/*
	for(i=0;i<10;i++){
		for(j=0;j<10;j++){
			printf("%d ",initm.M[i][j]);
		}
		printf("\n");
	}*/
}
Matrix mul(Matrix a, Matrix b){ //让两个矩阵相乘
    Matrix c;
    int i, j,k;
    for (i = 0; i<10; i++){
        for (j = 0; j<10; j++){
            c.M[i][j] = 0;
            for (k = 0; k<10; k++){
                c.M[i][j] = (c.M[i][j] + a.M[i][k] * b.M[k][j] % m) % m;
            }
            c.M[i][j] %= m;
        }
    }
    return c;
}
int MatrixFastM(Matrix p,Matrix q,int k,int r){  //矩阵快速幂
    int k1=k;
	while (k>0){
        if (k & 1){
            p = mul(p, q);
        }
        q = mul(q, q);
        k >>= 1;
    }
    int nm = r - 10 * k1;
	/*int i,j;
	
	printf("p.M%d\n",nm);
	for(i=0;i<10;i++){
		for(j=0;j<10;j++){
			printf("%d ",p.M[i][j]);
		}
		printf("\n");
	}*/
    return p.M[0][nm];
}
int main()
{
    int k;
    int i, j;
    freopen("in.txt", "r", stdin);
    while (~scanf("%d%d", &k, &m)){
        memset(unitm.M, 0, sizeof(unitm.M));
        for (i = 0; i < 10; i++){
            unitm.M[0][i] = i;
        }
        for (i = 0; i < 10; i++){
            scanf("%d", &a[i]);
        }
        initM();
        int res = MatrixFastM(unitm, initm, k / 10,k);
        printf("%d\n", res%m);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值