您的位置:首页 > 其它

HDU1757 A Simple Math Problem

2016-03-17 18:23 323 查看

A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3821    Accepted Submission(s): 2314


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each case, output f(k) % m in one line.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

45
104

 

[align=left]Author[/align]
linle
 

[align=left]Source[/align]
2007省赛集训队练习赛(6)_linle专场

 

[align=left]Recommend[/align]
lcy   |   We have carefully selected several similar problems for you:  1575 1588 3117 2276 2256

#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define LL long long
using namespace std;
int n,m;
struct Matrix
{
int x,y;
int A[11][11];
void Clean()
{
x=y=0;
memset(A,0,sizeof(A));
}
Matrix operator + (const Matrix& b)const
{
Matrix tmp;
tmp.x=x,tmp.y=y;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
tmp.A[i][j]=A[i][j]+b.A[i][j];
}
}
return tmp;
}
Matrix operator -(const Matrix& b)const
{
Matrix tmp;
tmp.x=x;tmp.y=y;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
tmp.A[i][j]=A[i][j]-b.A[i][j];
}
}
return tmp;
}
Matrix operator *(const Matrix& b)const
{
Matrix tmp;
tmp.Clean();
tmp.x=x;tmp.y=b.y;//x代表行数,y代表列数;
for(int i=0;i<x;i++)
{
for(int j=0;j<tmp.y;j++)
{
for(int k=0;k<x;k++)
{
(tmp.A[i][j]+=A[i][k]*b.A[k][j])%=m;
}
}
}
return tmp;
}
}S;
int Matrixpower(int n)
{
Matrix ans;
ans.Clean();
ans.x=10;
ans.y=1;
for(int i=0;i<10;i++)
{
ans.A[i][0]=i;
}
S.Clean();
S.x=10,S.y=10;
for(int i=0;i<9;i++)
{
S.A[i][i+1]=1;
}
for(int i=9;i>=0;i--)
{
scanf("%d",&S.A[9][i]);
}
while(n)
{
if(n&1)
{
ans=S*ans;
}
S=S*S;
n>>=1;
}
return ans.A[9][0];
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n<10)
printf("%d\n",n%m);
else
printf("%d\n",Matrixpower(n-9));
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: