【HDU-oj】-1757-A Simple Math Problem(矩阵,十一阶)
2017-03-04 16:30
543 查看
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4503 Accepted Submission(s): 2714
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
题解:这里构造的矩阵是一个十一阶的,方法和之前的矩阵乘法一样。
A: Fn Fn-1......Fn-10
B: Fn-1 Fn-2......Fn-11
要由下面的矩阵的到上面的就要乘以一个十一阶的已知矩阵,根据矩阵乘法可以求出就这个矩阵为:
暂时记为c;若求n=11,则输出最上面矩阵的A[1][1];
用B*c^1就好。这样我们就需要知道B矩阵的值即:F10 F9 F8 ......F1 F0,可以根据题意求出
代码:
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int MOD;
struct Matrix
{
int h,w;
int m[15][15];
};
Matrix Matrix_multiply(Matrix a,Matrix b)
{
Matrix c;
c.h=a.h;
c.w=b.w;
CLR(c.m,0);
for(int i=1;i<=a.h;i++)
{
for(int j=1;j<=a.w;j++)
{
if(a.m[i][j]==0)
continue;
for(int k=1;k<=b.w;k++)
c.m[i][k]=(c.m[i][k]+a.m[i][j]*b.m[j][k]%MOD)%MOD;
}
}
return c;
}
Matrix Quick(Matrix a,int n)
{
Matrix ans;
ans.h=ans.w=a.h;
CLR(ans.m,0);
for(int i=1;i<=a.h;i++)
ans.m[i][i]=1;
while(n)
{
if(n&1)
ans=Matrix_multiply(ans,a);
n>>=1;
a=Matrix_multiply(a,a);
}
return ans;
}
int main()
{
int k;
int a[11];
while(~scanf("%d%d",&k,&MOD))
{
if(k<10)
printf("%d\n",k%MOD);
else
{
Matrix pr,now;
pr.h=pr.w=11;
CLR(pr.m,0);
for(int i=1;i<=10;i++)
{
scanf("%d",&a[i]);
pr.m[i][1]=a[i];
}
for(int i=2;i<=11;i++)
pr.m[i-1][i]=1;
now.h=1,now.w=11;
CLR(now.m,0);
now.m[1][1]=(9*a[1]+8*a[2]+7*a[3]+6*a[4]+5*a[5]+4*a[6]+3*a[7]+2*a[8]+a[9]);
for(int i=2;i<=10;i++)
now.m[1][i]=11-i;
Matrix ans;
ans=Matrix_multiply(now,Quick(pr,k-10));
printf("%d\n",ans.m[1][1]);
}
}
return 0;
}
相关文章推荐
- HDU - 1757 A Simple Math Problem (构造矩阵)
- HDU 1757 A Simple Math Problem 矩阵快速幂
- hdu 1757 A Simple Math Problem_矩阵快速幂
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
- HDU 1757 A Simple Math Problem(矩阵快速幂)
- HDU 1757 A Simple Math Problem(矩阵高速幂)
- hdu 1757 A Simple Math Problem_矩阵快速幂
- hdu 1757 A Simple Math Problem (矩阵快速幂)
- hdu 1757 A Simple Math Problem(矩阵快速幂)
- HDU 1757 A Simple Math Problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem 矩阵基础题
- hdu 1757 A Simple Math Problem(矩阵优化递推)
- HDU 1757-A Simple Math Problem(矩阵快速幂)
- HDU 1757 A Simple Math Problem (矩阵快速幂)
- HDU 1757 A Simple Math Problem(构造矩阵)
- HDU 1757 A Simple Math Problem(矩阵快速幂)
- hdu A Simple Math Problem 1757 矩阵的构造和快速幂
- HDU 1757 A Simple Math Problem 矩阵快速幂
- 【矩阵乘法】hdu hdoj 1757 A Simple Math Problem
- HDU 1757 A Simple Math Problem 矩阵快速幂