HDU 6050 Funny Function

本文介绍了一种使用矩阵快速幂解决特定数学问题的方法,并通过一个具体的编程实例详细展示了如何实现该算法。文章针对一个给定的函数求解问题,通过发现规律并利用矩阵运算进行高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Funny Function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1215    Accepted Submission(s): 596


Problem Description
Function  Fx,y satisfies:


For given integers N and M,calculate 
Fm,1  modulo 1e9+7.
 

Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
 

Output
For each given N and M,print the answer in a single line.
 

Sample Input
  
  
2 2 2 3 3
 

Sample Output
  
  
2 33

题意:求f m 1

题解:打表找到规律。。。

然后矩阵快速幂一步一步一步一步一步一步去完成

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
ll N;
struct Matrix{
    ll a[4][4];
};
Matrix mul(Matrix a,Matrix b){
    Matrix c;
    memset(c.a,0,sizeof(c.a));
    for(ll i=1;i<=N;i++){
        for(ll j=1;j<=N;j++){
            if(!a.a[i][j])continue;
            for(ll k=1;k<=N;k++){
                c.a[i][k]+=a.a[i][j]*b.a[j][k];
                c.a[i][k]%=mod;
            }
        }
    }
    return c;
}
Matrix quick(Matrix a,ll k){
    Matrix ans;
    memset(ans.a,0,sizeof(ans));
    for(ll i=1;i<=N;i++)ans.a[i][i]=1;
    while(k>0){
        if(k&1)ans=mul(ans,a);
        a=mul(a,a);
        k/=2;
    }
    return ans;
}
ll qq(ll a,ll k){
    ll ans=1;
    while(k){
        if(k&1)ans=ans*a%mod;
        a=a*a%mod;
        k/=2;
    }
    return ans;
}
int main(){
    int t,cas=1;
    scanf("%d",&t);
    while(t--){
        ll n,m;
        scanf("%lld%lld",&n,&m);
        if(m==1){
            printf("1\n");
            continue;
        }
        Matrix f2;
        memset(f2.a,0,sizeof(f2.a));
        f2.a[1][1]=f2.a[1][2]=1;
        f2.a[2][1]=2;
        N=2;
        f2=quick(f2,n-1);
        ll ps=f2.a[1][1]+f2.a[2][1];
        if(n%2==0)ps--;
        ps%=mod;
        ps+=mod;
        ps%=mod;
//        cout<<ps<<endl;
        if(n%2){
            ll tt=0;
            ll mo=(n+1)/2;
            memset(f2.a,0,sizeof(f2.a));
            f2.a[1][1]=4;
            f2.a[2][1]=2;
            f2.a[2][2]=1;
            N=2;
            f2=quick(f2,mo-1);
            ll cha=f2.a[2][1];
            cha%=mod;
            ll muls=(qq(2,n)-1+mod)%mod;
            N=3;
            memset(f2.a,0,sizeof(f2.a));
            f2.a[1][2]=1;
            f2.a[1][1]=muls;
            f2.a[3][1]=cha;
            f2.a[3][3]=1;
            f2=quick(f2,m-2);
            ps=ps*f2.a[1][1]-f2.a[3][1];
            ps=(ps%mod+mod)%mod;
            cout<<ps<<endl;
        }
        else{
            ps=ps*qq((qq(2,n)-1+mod)%mod,m-2)%mod;
            cout<<ps<<endl;
        }
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值