A Simple Math Problem 矩阵打水题

                              A Simple Math Problem

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF = 0x4fffffff;
17 const double EXP = 1e-5;
18 const int MS = 1005;
19 const int SIZE = 10;
20
21 typedef vector<vector<int> > mat;
22
23 mat matrix(SIZE,vector<int>(SIZE));
24
25 int n,mod;
26
27 mat mul(mat &m1,mat &m2)
28 {
29     mat m(SIZE,vector<int>(SIZE));
30     for(int i=0;i<SIZE;i++)
31         for(int j=0;j<SIZE;j++)
32             for(int k=0;k<SIZE;k++)
33         {
34             m[i][j]=(m1[i][k]*m2[k][j]+m[i][j])%mod;
35         }
36     return m;
37 }
38
39 mat pow(int n)
40 {
41     mat m(SIZE,vector<int>(SIZE));
42     for(int i=0;i<SIZE;i++)
43         m[i][i]=1;
44     while(n)
45     {
46         if(n&1)
47             m=mul(m,matrix);
48         matrix=mul(matrix,matrix);
49         n>>=1;
50     }
51     return m;
52 }
53
54 int main()
55 {
56     while(scanf("%d%d",&n,&mod)!=EOF)
57     {
58         for(int i=0;i<SIZE;i++)
59             for(int j=0;j<SIZE;j++)
60                 matrix[i][j]=(i-1)==j;
61         for(int i=0;i<SIZE;i++)
62             scanf("%d",&matrix[0][i]);
63
64         if(n<10)
65         {
66             printf("%d\n",n%mod);
67             continue;
68         }
69         matrix=pow(n-9);
70         int ans=0;
71         for(int i=0;i<SIZE;i++)
72             ans+=matrix[0][i]*(9-i)%mod;
73         printf("%d\n",ans%mod);
74     }
75 }
时间: 2024-09-28 14:01:09

A Simple Math Problem 矩阵打水题的相关文章

hdu1757 A Simple Math Problem(矩阵快速幂)

题目: A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f

hdu 1757 A Simple Math Problem 矩阵

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2831    Accepted Submission(s): 1693 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x)

hdu1757---A Simple Math Problem(矩阵)

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 pos

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 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 w

Hdu 1757A Simple Math Problem矩阵

构造个矩阵搞下就行了 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 1  0   0   0  0   0   0  0   0  0 0  1   0   0  0   0   0  0   0  0 0  0   1   0  0   0   0  0   0  0 0  0   0   1  0   0   0  0   0  0 0  0   0   0  1   0   0  0   0  0 0  0   0   0  0   1   0  0   0  0 0  0

hdu 1757 A Simple Math Problem 矩阵优化+快速幂

#include<bits/stdc++.h> using namespace std; typedef long long LL; const int n=10; int kt,m; struct Matrix { int mp[n][n]; }; Matrix mul(Matrix a,Matrix b) { int i,j,k; Matrix c; for(i=0; i<10; i++) for(j=0; j<10; j++) { c.mp[i][j]=0; for(k=0;

矩阵十题【八】 HDU 1715 A Simple Math Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 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); 给出k,m和a0~a9,求f(k)%m,  k<2*10^9 , m < 10^5 这是一个递推式,故可以用矩阵乘法来求 和上题类似,具体思路过程见上题

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =