您的位置:首页 > 其它

A Simple Math Problem(HDU 1757 构造矩阵)

2016-05-06 12:00 381 查看
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 .

[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



#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int k,n;
struct Matrix
{
int mat[10][10];
}p;
int aa[10];
Matrix mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
c.mat[i][j]=0;
for(int k=0;k<10;k++)
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%n;
}
}
return c;
}
Matrix mod_pow(Matrix x,int n)
{
Matrix res;
memset(res.mat,0,sizeof(res.mat));
for(int i=0;i<10;i++)
res.mat[i][i]=1;
while(n)
{
if(n&1)
res=mul(res,x);
x=mul(x,x);
n>>=1;
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d%d",&k,&n)!=EOF)
{
if(k<10)
{
printf("%d\n",k);
continue;
}
memset(p.mat,0,sizeof(p.mat));
for(int i=0;i<10;i++)
cin>>p.mat[0][i];
for(int i=1;i<10;i++)
p.mat[i][i-1]=1;
Matrix ans= mod_pow(p,k-9);
/*for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
cout<<ans.mat[i][j]<<" ";
cout<<endl;
}*/
int sum=0;
for(int i=0;i<10;i++)
sum+=ans.mat[0][i]*(9-i);
printf("%d\n",sum%n);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: