进来递推(矩阵快速幂+逆元)

Description

Input

Output
每组输出一个整数表示答案
Sample Input

1

4 6 2

Sample Output

48

Hint

Source
“科林明伦杯”哈尔滨理工大学第13届程序设计团队赛

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>PII;
constexpr int N = 2,mod=1e9+7;
int gcd(int a,int b){
    return b ? gcd(b,a%b):a;
}
void extend_gcd(int a,int b,int &x,int &y){
    if(b==0){
        x=1,y=0;
        return;
    }
    extend_gcd(b,a%b,x,y);
    int tem=x;
    x=y;
    y=tem-(a/b)*y;
}
int mod_inv(int a,int mod){
    int x,y;
    extend_gcd(a,mod,x,y);
    return (x%mod+mod)%mod;
}
void mul(int c[], int a[], int b[][N])
{
    int temp[N] = {0};
    for (int i = 0; i < N; i++) // i代表的是坐标
    {
        for (int j = 0; j < N; j++) // j代表操作;
        {
            temp[i] = (temp[i] + a[j] * b[j][i]) % mod;
        }
    }
    memcpy(c, temp, sizeof temp);
}
void mul(int c[][N], int a[][N], int b[][N])
{
    int temp[N][N] = {0};
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            for (int k = 0; k < N; k++)
            {
                temp[i][j] = (temp[i][j] + a[i][k] * b[k][j]) % mod;
            }
        }
    }
    memcpy(c, temp, sizeof temp);
}
signed main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
   int t;
   cin>>t;
   while(t--){
       int t1,t2,k;
       cin>>t1>>t2>>k;
       int f1[3] = {t1,t2};
       int A[N][N] = {
               {0, 1,},
               {1, 2},
       };
       k--;
       while (k)
       {
           if (k & 1)
           {
               mul(f1, f1, A);
           }
           mul(A, A, A);
           k >>= 1;
       }
       int d=gcd(t1,t2);
       d=mod_inv(d,mod);
       int c=f1[0]*f1[1]%mod;
       cout<<(c*d)%mod<<endl;
   }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值