hdu 4565 So Easy!

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4565

可以参考hdu2256解法类似:http://blog.csdn.net/hearthougan/article/details/24848401

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

struct Matrix
{
    LL iMatrix[2][2];
};

Matrix iPer, iCell;
LL a, b, n, iMod;

void Inite_Matrix()
{
    iPer.iMatrix[0][0] = iPer.iMatrix[1][1] = 1, iPer.iMatrix[0][1] = iPer.iMatrix[1][0] = 0;
    iCell.iMatrix[0][0] = iCell.iMatrix[1][1] = a, iCell.iMatrix[0][1] = b, iCell.iMatrix[1][0] = 1;
}

Matrix Multi_Matrix(Matrix a, Matrix b)
{
    Matrix c;
    int i, j, k;
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 2; ++j)
        {
            c.iMatrix[i][j] = 0;
            for(k = 0; k < 2; ++k)
            {
                c.iMatrix[i][j] = (c.iMatrix[i][j]%iMod + ((a.iMatrix[i][k]%iMod) * (b.iMatrix[k][j]%iMod))%iMod)%iMod;
            }
        }
    }
    return c;
}

Matrix Quick_Mod_Matrix(LL n)
{
    if(n == 1)
        return iCell;
    Matrix c, tmp;
    tmp = iCell;
    c = iPer;
    while(n)
    {
        if(n&1)
        {
            c = Multi_Matrix(c, tmp);
            n--;
        }
        n >>= 1;
        tmp = Multi_Matrix(tmp, tmp);
    }
    return c;
}

int main()
{
    Matrix c;
    while(~scanf("%lld %lld %lld %lld", &a, &b, &n, &iMod))
    {
        Inite_Matrix();
        c = Quick_Mod_Matrix(n);
        printf("%lld\n", (2*(c.iMatrix[0][0]%iMod)%iMod));
    }
    return 0;
}


阅读更多
版权声明:本文为博主原创文章,转载需注明出处。 https://blog.csdn.net/Hearthougan/article/details/24866869
个人分类: hdu 数论 数学题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭