hdu 2855 Fibonacci Check-up【递推+矩阵快速幂】

Fibonacci Check-up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1306    Accepted Submission(s): 741

Problem Description

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 , is the combination number. The answer mod m (the total number of alpc team members) is just your alpc-number.

 

 

Input

First line is the testcase T.
Following T lines, each line is two integers n, m ( 0<= n <= 10^9, 1 <= m <= 30000 )

 

 

Output

Output the alpc-number.

 

 

Sample Input

2

1 30000

2 30000

 

 

Sample Output

1

3

 

 

Source

2009 Multi-University Training Contest 5 - Host by NUDT

 

 对于这样一个公式:


 一开始真的很没有头绪,对于这样一个组合公式,拆分再组合,组合再拆分,做了很长一段时间都没有结果,后来干脆暴力打表找规律,然后写了一发暴力的代码,打了0-10的表,其内容是这样的:

0 1 3 8 21 55 144 377 987 2584 6765


然后就开始找规律,找啊找啊找规律。发现8是从3*3-1来的,21是从8*3-3来的,55是从21*3-8来的,一瞬间整个人都好了起来,然后写递推式子,写了一发矩阵快速幂,开开心心的发现WA到死。。。。。其实不难发现为何会wa,数据是成几何倍数增加的,我这样里边的矩阵乘法会让一个数据彻底炸掉。后来修改了修改代码,无果,放弃。换个思路。


后来再发现,1 3 8 21 55这些个数也都是斐波那契数啊,而且Sn=F2*n,比如S3=8=F6............然后再敲,1A。

构建矩阵是要这样构建的:

hdu 2855 Fibonacci Check-up【递推+矩阵快速幂】_第1张图片

剩下的就是代码实现了:


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define ll long long int
ll n,mod;
typedef struct Matrix
{
    ll mat[2][2];
}matrix;
matrix A,B;
Matrix matrix_mul(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    int i,j,k;
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            for(int k=0;k<2;k++)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                c.mat[i][j]%=mod;
            }
        }
    }
    return c;
}
Matrix matrix_quick_power(matrix a,ll k)//矩阵快速幂0.0
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0;i<2;i++)
    b.mat[i][i]=1;//单位矩阵b
    while(k)
    {
        if(k%2==1)
        {
            b=matrix_mul(a,b);
            k-=1;
        }
        else
        {
            a=matrix_mul(a,a);
            k/=2;
        }
    }
    return b;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d",&n,&mod);
        A.mat[0][0]=1;A.mat[0][1]=1;//我们通过推论得到的矩阵A
        A.mat[1][0]=1;A.mat[1][1]=0;
        B=matrix_quick_power(A,n*2);
        cout<<B.mat[0][1]<<endl;
    }
}













你可能感兴趣的:(HDU,HDU,杭电,2855,2855)