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 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.

题意描述:这题题意没什么说的吧,如那个公式所示,给出k和m,然后求f(k)%m。

算法分析:看到k这么大,肯定暴力是不行的,这里得运用到矩阵快速幂。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9
10 int K,m;
11 struct matrix
12 {
13     int an[12][12];
14 }temp;
15
16 matrix multiply(matrix a,matrix b)
17 {
18     matrix c;
19     memset(c.an,0,sizeof(c.an));
20     for (int i=1 ;i<=10 ;i++)
21     {
22         for (int j=1 ;j<=10 ;j++)
23         {
24             for (int k=1 ;k<=10 ;k++)
25             {
26                 c.an[i][j]=(c.an[i][j]+a.an[i][k] * b.an[k][j])%m;
27                 c.an[i][j] %= m;
28             }
29         }
30     }
31     return c;
32 }
33
34 int calc(int u)
35 {
36     matrix x;
37     memset(x.an,0,sizeof(x.an));
38     for (int j=1 ;j<=10 ;j++) x.an[j][1]=10-j;
39     while (u)
40     {
41         if (u&1) x=multiply(temp,x);
42         u >>= 1;
43         temp=multiply(temp,temp);
44     }
45     int sum=0;
46     return x.an[1][1]%m;
47 }
48
49 int main()
50 {
51     while (scanf("%d%d",&K,&m)!=EOF)
52     {
53         for (int i=1 ;i<=10 ;i++) scanf("%d",&temp.an[1][i]);
54         if (K<=9) {printf("%d\n",K);continue; }
55         for (int i=2 ;i<=10 ;i++)
56         {
57             for (int j=1 ;j<=10 ;j++)
58                 temp.an[i][j]= i==j+1 ? 1 : 0 ;
59         }
60         printf("%d\n",calc(K-9));
61     }
62     return 0;
63 }
时间: 2025-01-03 18:39:37

hdu 1757 A Simple Math Problem 矩阵快速幂的相关文章

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(矩阵快速幂)

题目: 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 矩阵优化+快速幂

#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 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) =

HDU - 1757 A Simple Math 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 in

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: 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); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

hdu 1757 A Simple Math Problem 构造矩阵

题意:函数f(x), 若 x < 10 f(x) = x. 若 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 且 ai(0<=i<=9) 仅为 0 或 1 . 给定k,m,求f(k)%m; 思路:求一个递推函数的函数值,显然是矩阵快速幂,矩阵构造方法如下: #include<cstdio> #include<cstring> #include<al

hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)

题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10) 同时ai(0<=i<=9) 不是 0 就是 1: 现在给你 ai 的数字,以及k和mod,请你算出 f(x)%mod 的结果是多少 思路:线性递推关系是组合计数中常用的一种递推关系,如果直接利用递推式,需要很长的时间才能计算得出,时间无法承受,但是现在我们已知

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): 108 Accepted Submission(s): 77   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If