CSU - 1895(矩阵快速幂(a+b*sqrt(x))的通用方法+找规律)

Apache is a student of CSU. There is a math class every Sunday morning, but he is a very hard man who learns late every night. Unfortunate, he was late for maths on Monday. Last week the math teacher gave a question to let him answer as a punishment, but he was easily resolved. So the math teacher prepared a problem for him to solve. Although Apache is very smart, but also was stumped. So he wants to ask you to solve the problem. Questions are as follows: You can find a m made (1 + sqrt (2)) ^ n can be decomposed into sqrt (m) + sqrt (m-1), if you can output m% 100,000,007 otherwise output No.

Input

There are multiply cases. Each case is a line of n. (|n| <= 10 ^ 18)

Output

Line, if there is no such m output No, otherwise output m% 100,000,007.

Sample Input
2
Sample Output
9

题意:给出n,问是否能找到(1+sqrt(2))^n,然后找到一个m使得(1+sqrt(2))^n=sqrt(m)+sqrt(m-1)。

这道题有两种解法(我知道的),我第一次做这个题是这么推得:

开始看这道题要先去推公式,(1+sqrt(2))^n=sqrt(m)+sqrt(m-1),因为知道(1+sqrt(2)),所以我就先算这个式子,写成(1+sqrt(2))^2=sqrt(2)+sqrt(1)

(1+sqrt(2))^3=sqrt(9)+sqrt(8)

(1+sqrt(2))^4=sqrt(50)+sqrt(49)

(1+sqrt(2))^5=sqrt(289)+sqrt(288)

这里可以发现1,9,49,289是1,3,7,17的倍数,并且式子中的另一个数和他们是加一或者减一的关系,通过观察可以得知,当n是奇数m为次方数减一,当n为偶数,m为次方数加一,而通过1,3,7,17可以推出f(n)=f(n-1)*2+f(n-2)。这个递推式可以用矩阵快速幂求得结果,然后再利用我们找到的m与n的就的关系求的答案。

可能说的有点乱,就是先通过式子找到关于m的规律,然后找m与nd的关系,最后球的答案,上边的递推式刚好可以求得sqrt(m)+sqrt(m-1)中的m,而具体的m值要通过与n的关系来判断。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const ll mod=1e8+7;
ll n;
struct mat
{
    ll mm[5][5];
};
mat operator*(mat a,mat b)
{
    mat c;
    memset(c.mm,0,sizeof(c.mm));
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            for(int k=0;k<2;k++)
            {
                c.mm[i][j]+=a.mm[i][k]*b.mm[j][k];
                c.mm[i][j]%=mod;
            }
        }
    }
    return c;
}
ll pow(ll nn)
{
    mat ans,res;
    memset(ans.mm,0,sizeof(ans.mm));
    res.mm[0][0]=2;
    res.mm[0][1]=1;
    res.mm[1][0]=1;
    res.mm[1][1]=0;
    ans.mm[0][0]=3;
    ans.mm[0][1]=1;
    while(nn)
    {
        if(nn%2==1)
            ans=ans*res;
        res=res*res;
        nn/=2;
    }
    return ans.mm[0][0];
}
int main()
{
    while(~scanf("%lld",&n))
    {
        if(n<0)
            printf("No\n");
        else if(n==0)
            printf("1\n");
        else if(n==1)
            printf("2\n");
        else if(n==2)
            printf("9\n");
        else
        {
            n-=2;
            ll sum=pow(n);
            if(n%2==1)
                printf("%lld\n",(sum*sum)%mod+1);
            else printf("%lld\n",(sum*sum)%mod);
        }
    }
}

第二种,这种是上次做题:hdu2256学到的方法,当时是看了别人的博客发现好神奇,我就转载了人家的博客,印象比较深,这次就试了下这个方法,发现可行。

通用方法:当我们碰到(c+d*sqrt(x))^n这种式子,我们要考虑先让(c+d*sqrt(x))^2相乘一下,会得到一个an+bn*sqrt(x)的式子,然后找an,bn与a<n-1>,b<n-1>的关系(<n-1>代表的是下标,就是上一项,因为不会把n-1弄小变成下标只能出此下策)。

an+bn*sqrt(x)=(a<n-1>+b<n-1>*sqrt(x))*(c+d*sqrt(x)))能转换成an+bn*sqrt(x)=c*a<n-1>+d*x*b<n-1>+(d*a<n-1>+c*b<n-1>)*sqrt(x).有这个式子就能写出来矩阵快速幂的关系矩阵了就可以求得(c+d*sqrt(x))^n。

那么这道题就可以这么来,通过上边描述的方法,可以求得(1+sqrt(2))^n,因为(1+sqrt(2))^n=sqrt(m)+sqrt(m-1),由(1+sqrt(2))^n=an+bn*sqrt(2)=sqrt(an*an)+sqrt(bn*bn*2)可知an*an=m或者m-1,只要对比(an*an)和(bn*bn*2)的大小就可以得知哪个是m哪个是m-1。我们同过矩阵快速幂可以求得an和bn。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const ll mod=1e8+7;
struct mat
{
    ll mm[5][5];
};
mat operator*(mat a,mat b)
{
    mat c;
    memset(c.mm,0,sizeof(c.mm));
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            for(int k=0;k<2;k++)
            {
                c.mm[i][j]+=a.mm[i][k]*b.mm[k][j];
                c.mm[i][j]%=mod;
            }
        }
    }
    return c;
}
void pow(ll nn)
{
    mat res,ans;
    memset(ans.mm,0,sizeof(ans.mm));
    res.mm[0][0]=1;
    res.mm[0][1]=1;
    res.mm[1][0]=2;
    res.mm[1][1]=1;
    ans.mm[0][0]=1;
    ans.mm[0][1]=1;
    while(nn)
    {
        if(nn%2==1)
            ans=ans*res;
        res=res*res;
        nn/=2;
    }
    ll m1=(ans.mm[0][0]*ans.mm[0][0])%mod;
    ll m2=(ans.mm[0][1]*ans.mm[0][1]*2)%mod;
    if(m1<m2)
        printf("%lld\n",m2);
    else
        printf("%lld\n",m1);
}
int main()
{
    ll n;
    while(~scanf("%lld",&n))
    {
        if(n<0)
            printf("No\n");
        else if(n==0)
            printf("1\n");
        else
        {
            n--;
            pow(n);
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值