HDU 4565 So Easy!(构造共轭式+矩阵)(好题)

题意:

已给a,b是正数, 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.

求:

那是向上取整符号

思路:

注意到(a-1)2< b < a2

而且(a+sqrt(b))^n与其共轭式的和显然为整数,又注意到它的共轭式(a-sqrt(b))^n小于1(由于a,b大小关系)

所以即求Sn=(a+sqrt(b))^n + (a-sqrt(b))^n

再变形(易变形)递推Sn=2*aSn-1 + (b-a^2)*Sn-2 用快速幂求即可(详见http://www.klogk.com/articles/2013-06-02/hdu4565/

//78MS 1616K 1095 B C++
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a, b, n, mod;
struct mat
{
    ll a[3][3];
    mat()
    {
        memset(a,0,sizeof(a));
    }
};
mat I;

mat mul(mat m1,mat m2)
{
    mat ans;
    for(int i=1;i<=2;i++)
        for(int j=1;j<=2;j++)
            for(int k=1;k<=2;k++)
                ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%mod;
    return ans;
}

mat quickmul(mat m,int k)
{
    if(k<=0) return m;
    mat ans;
    for(int i=1;i<=2;i++) ans.a[i][i]=1;
    while(k)
    {
        if(k&1) ans=mul(ans,m);
        m=mul(m,m);
        k>>=1;
    }
    return ans;
}

int main()
{
    while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&mod))
    {
        I.a[1][1]=0,I.a[1][2]=1,I.a[2][1]=((b-a*a)%mod+mod)%mod,I.a[2][2]=2*a%mod;
        ll c1=2*a%mod,c2=(2*a*a+2*b)%mod;
        mat t=quickmul(I,n-2);
        int ans=(t.a[2][1]*c1+t.a[2][2]*c2)%mod;
        if(n>2) printf("%d\n",ans);
        else printf("%I64d\n",n==1? c1:c2);
    }
    return 0;
}

时间: 2024-07-31 00:03:18

HDU 4565 So Easy!(构造共轭式+矩阵)(好题)的相关文章

HDU 4565 So Easy!

线性推,矩阵乘法+快速幂求通项. 传送门:点击打开链接 #include <cstdio> #include <cstring> #include <cmath> #include <iostream> using namespace std; #define LL long long struct Mat{ LL f[2][2]; }; LL MOD; Mat mul(Mat a,Mat b) { LL i,j,k; Mat c; memset(c.f,0

hdu 4565 So Easy! (共轭构造+矩阵快速幂)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求余,所以用矩阵快速幂加速求解过程的时候,会产生误差,就很自然地想到了凑数,因为(a-1)^2<b<a^2,得出0<a-sqrt(b)<1,则无论n取多大,(a-sqrt(b))^n都是小于1的,(a-sqrt(b))^n 与 (a+sqrt(b))^n共轭,两者展开后会相互抵销,所以(

【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)

链接 :click here~~ 题意: A sequence Sn 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 Sn. You, a top coder, say: So easy! [解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下

HDU 4565 So Easy! 矩阵快速幂 + 共轭数

题意:中文题 So Easy 解题思路: 这题应该是 HDU 2256的原题 , 根据类似的结论可以推出 中间矩阵 ta  1 tb ta 原矩阵 1 1 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月17日 星期三 11时35分45秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #inc

HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简

http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为(a - 1)^2 < b < (a ^ 2),所以其不是完全平方数,假如是,那么设其为c,则有a - 1 < c < a,这是矛盾的 所以,向上取整这个步骤,是必不可少的了. 那么,我在它后面加上一个< 1的数,同时使得它们结合成为整数,那就相当于帮它取整了.根据二项式定理 (

矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Google Codejam Round 1A的C题. #include <bits/stdc++.h> typedef long long ll; const int N = 5; int a, b, n, mod; /* *矩阵快速幂处理线性递推关系f(n)=a1f(n-1)+a2f(n-2)+.

数学(矩阵乘法):HDU 4565 So Easy!

So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3804    Accepted Submission(s): 1251 Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce

hdu 4565 So Easy! 矩阵

So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2350    Accepted Submission(s): 729 Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce

HDU 4565 So Easy! 矩阵快速幂

设(a+sqrt(b))^n为(Xn + Yn*sqrt(b)),那么显然有(a+sqrt(b))^(n+1) 为 (a*Xn + b*Yn + (aYn+Xn)*sqrt(b)). 那么显然有(a+sqrt(b))的Xn,Yn可以表示为 : 然后又会发现,(a-sqrt(6))^n可以表示为: 那么会发现(a+sqrt(b))^n = (a+sqrt(b))^n + (a-sqrt(b))^n - (a-sqrt(b))^n = Xn+Yn*sqrt(b) +Xn-Yn*sqrt(b) -(a