hdu 4565 So Easy!

原题目
A sequence S n is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
  You, a top coder, say: So easy!

Input
  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 2 15, (a-1) 2< b < a 2, 0 < b, n < 2 31.The input will finish with the end of file.

Output
  For each the case, output an integer S n.

解释:给出一个公式:
要求小数点后一但有数往上进位(只保留整数位上的数);求出Sn

题解:简单的矩阵快速幂,可以直接套模板过;
本题主要事考察能否在短时间内观察出题目所给的公式的规律:通过找共轭来规避小数点后的计算;
建立矩阵 e :{2*a | -(a^2-b)}
:{1 | 0 }
矩阵 c {2*a}
{1}
本题要求的便是e^(n-1)*矩阵 c的结果;
给出代码

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define maxn 100007
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll a,b,n,m;
struct mat
{
    ll val[5][5];
};
mat matmult(mat a,mat b)
{
    mat ms;
    memset(ms.val,0,sizeof(ms.val));
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            for(int k=1;k<=2;k++)
            {
                ms.val[i][j]+=a.val[i][k]*b.val[k][j];
                ms.val[i][j]%=m;
            }
        }
    }
    return ms;
}
mat matpow(mat a,ll k)
{
    mat ans;
    memset(ans.val,0,sizeof(ans.val));
    for(int i=1;i<=2;i++)
        ans.val[i][i]=1;
    while(k)
    {
        if(k&1)
            ans=matmult(a,ans);
        a=matmult(a,a);
        k>>=1;
    }
    return ans;
}
int main()
{
    while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m)==4)
    {
        mat c;
        mat e;
        e.val[1][1]=2*a%m;
        e.val[1][2]=-(a*a-b)%m;
        e.val[2][1]=1;
        e.val[2][2]=0;
        if(n==1)
        {
            printf("%lld\n",2*a%m);
            continue;
        }
        e=matpow(e,n-1);
        printf("%lld\n",((e.val[1][1]*2*a+e.val[1][2]*2)%m+m)%m);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值