HDU 4565 So Easy!

本题题意:
给出整数a,b,n,m求出Sn。
l0<a, m < 2 15, (a-1) 2<b < a 2, 0 < b, n < 2 31
解题思路:
因为a,b,n很大所以如果直接求(a+sqrt(b))^n会爆double范围 所以只能对公式化简。由题中b的范围为(a-1) 2<b < a 2   知道(a-1)<sqrt(b)<a,所以我们可以得出0<a-sqrt(b)<1,即有共轭的性质可知(a+sqrt(b))^n+(a-sqrt(b))^n 为整数并等于(a+sqrt(b))^n向上取整所以设Sn=[(a+sqrt(b))^n+(a-sqrt(b))^n]%m 我们可以两边同时乘以(a+sqrt(b))+(a-sqrt(b)) 可化简为Sn+1=(2*a)Sn-(a*a-b)Sn-1;所以我们在用矩阵快速幂就可以做了
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const double eps=1e-6;
const int MAX = 2;
ll mod;
typedef  struct
{
    ll  m[MAX][MAX];
}Matrix;
Matrix P;//构造矩阵
Matrix I;//原始矩阵*/
Matrix matrixmul(Matrix a,Matrix b)
{
    int i,j,k;
    Matrix c;
    for (i = 0 ;  i < MAX;  i++)
        for (j = 0;  j < MAX; j++)
            
        {
            c.m[i][j] = 0;
            for (k = 0;  k< MAX;  k++)
                c.m[i][j]=(c.m[i][j] +(a.m[i][k] * b.m[k][j])%mod+mod)%mod;
        }
    return c;
}

Matrix quickpow(long long n)
{
    Matrix m =P,b=I;
    while (n)
    {
        if(n&1)
            b = matrixmul(b,m);
        n = n/2;
        m = matrixmul(m,m);
    }
    return b;
}
long long a,b,m,n;
ll s[3];
void init(){
    s[1]=(a+sqrt(b))+a-sqrt(b)+eps;
    s[2]=pow(a+sqrt(b),2)+pow(a-sqrt(b),2)+eps;
   // cout<<s[1]<<' '<<s[2]<<endl;
    mod=m;
    P={2*a,1ll,-(a*a-b),0};
    I={s[2],s[1],0,0};
}
int main(int argc, const char * argv[]) {
 
    while (cin>>a>>b>>n>>m) {
        init();
        if (n<=2) {
            cout<<(s[n])%m<<endl;
        }
        else {
            Matrix c=quickpow(n-2);
           // double ss=s[2]*c.m[0][0]+s[1]*c.m[1][0];
            cout<<c.m[0][0]%m<<endl;
        }
        
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值