您的位置:首页 > 其它

【HDU 2855】 Fibonacci Check-up (矩阵乘法)

2016-09-28 14:06 423 查看

Fibonacci Check-up

[align=left]Problem Description[/align]
Every ALPC has his own alpc-number just like alpc12, alpc55, alpc62 etc.
As more and more fresh man join us. How to number them? And how to avoid their alpc-number conflicted?
Of course, we can number them one by one, but that’s too bored! So ALPCs use another method called Fibonacci Check-up in spite of collision.

First you should multiply all digit of your studying number to get a number n (maybe huge).
Then use Fibonacci Check-up!
Fibonacci sequence is well-known to everyone. People define Fibonacci sequence as follows: F(0) = 0, F(1) = 1. F(n) = F(n-1) + F(n-2), n>=2. It’s easy for us to calculate F(n) mod m.
But in this method we make the problem has more challenge. We calculate the formula

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;

struct node
{
int a[5][5];
}t[5];

int n,m;

void init()
{
t[0].a[1][1]=1;t[0].a[1][2]=1;
t[0].a[2][1]=1;t[0].a[2][2]=2;
}

void mul(int x,int y,int z)
{
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
{
t[2].a[i][j]=0;
for(int k=1;k<=2;k++)
t[2].a[i][j]=(t[2].a[i][j]+t[y].a[i][k]*t[z].a[k][j])%m;
}
t[x]=t[2];
}

void get_un()
{
memset(t[1].a,0,sizeof(t[1].a));
for(int i=1;i<=2;i++) t[1].a[i][i]=1;
}

void qpow(int b)
{
get_un();
while(b)
{
if(b&1) mul(1,0,1);
mul(0,0,0);
b>>=1;
}
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
qpow(n);
printf("%d\n",t[1].a[1][2]);
}
return 0;
}


[HDU 2855]

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