hdu 2256【矩阵快速幂 构造】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256

Problem Description

这里写图片描述

Input
The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)

Output
For each input case, you should output the answer in one line.

Sample Input
3
1
2
5

Sample Output
9
97
841

Source
HDOJ 2008 Summer Exercise(4)- Buffet Dinner

题意:
(2+3)2n %1024向下取整的值

思路:
首先平方拿下来,即求 (5+26)n %1024, (5+26)n = an+bn6
F(n)=an+bn6

F(n)=an+bn6F(n1)=an1+bn16F(n)=(5+26)F(n1)

联立可以推出矩阵为 (52125) * (an1bn1) = (anbn)
到此并没有结束,由于出现了根号,double取模存在误差,所以不能正常取模,应当求向下取整的值。

(5+26)n+(526)n=2an 62.4 ,因此 (526)n0 (5+26)n 向下取整等于2 an1 .

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<ctype.h>
#include<time.h>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#include<map>
#include<vector>
#include<sstream>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
void fre(){freopen("in.txt","r",stdin);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define zero(a) fabs(a)<=eps
#define equal(a,b) zero(a-b)
const ld pi=acos(-1);
double e=2.718281828;
const double eps=1e-6;
const int MOD=1024;
const int N=2;
//ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

struct Matrix{
    int mat[N][N];
    Matrix operator*(const Matrix& m)const{
        Matrix tmp;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                tmp.mat[i][j]=0;
                for(int k=0;k<N;k++)
                {
                    tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%MOD;
                    tmp.mat[i][j]%=MOD;
                }
            }
        }
        return tmp;
    }
};

int Pow(Matrix &m,int k)
{
    if(k==1)
        return 9;
    k--;
    Matrix ans;
    memset(ans.mat,0,sizeof(ans.mat));
    for(int i=0;i<N;i++)
        ans.mat[i][i]=1;
    while(k)
    {
        if(k&1)
            ans=ans*m;
        k>>=1;
        m=m*m;
    }
    int x=(ans.mat[0][0]*5+ans.mat[0][1]*2)%MOD;
    return (2*x-1)%MOD;
}

int main()
{
    //fre();
    int cas,n;
    Matrix m;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        m.mat[0][0]=5;m.mat[1][1]=5;
        m.mat[1][0]=2;m.mat[0][1]=12;
        printf("%d\n",Pow(m,n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值