【A Simple Math Problem】【HDU - 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.

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

解题报告:求解一个奇妙的数学问题,特殊的函数,当参数是小于10的时候返回自己,然后大于10的话,满足等式 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10)。利用矩阵快速幂就可以求解,难点就是想到利用矩阵快速幂,中间的取模是关键,要不然会wa。

 

ac代码:
 

#include
#include
#include
#include
#define maxn 10
using namespace std;
typedef long long ll;
int k;
int m;
struct Mat{
	ll v[maxn][maxn];
	Mat ()
	{
		memset(v,0,sizeof(v));
		for(int i=0;i<10;i++)
			v[i][i]=1;
	}
};
Mat f;
Mat temp;
Mat mul(Mat a,Mat b)
{
	Mat res;
	for(int i=0;i>=1;
	}
	res=mul(res,a);
	return res.v[0][0];
}


int main()
{
	int i;
	for(i=0;i<10;i++)
	{
		f.v[i][0]=9-i;
		for(int j=1;j<10;j++)
			f.v[i][j]=0;
	}
		
	while(scanf("%d%d",&k,&m)!=EOF)
	{
		for(int i=0;i

 

你可能感兴趣的:(acm训练,数论)