【2018 Multi-University Training Contest 7】Sequence(分块+矩阵快速幂)

Problem Description
Let us define a sequence as below

F1F2Fn===ABCFn2+DFn1+Pn { F 1 = A F 2 = B F n = C ⋅ F n − 2 + D ⋅ F n − 1 + ⌊ P n ⌋

Your job is simple, for each task, you should output Fn module 109+7. 10 9 + 7.

Input
The first line has only one integer T, indicates the number of tasks.

Then, for the next T lines, each line consists of 6 integers, A , B, C, D, P, n.

1T200A,B,C,D1091P,n109 1 ≤ T ≤ 200 ≤ A , B , C , D ≤ 10 9 1 ≤ P , n ≤ 10 9

Sample Input
2
3 3 2 1 3 5
3 2 2 2 1 4

Sample Output
36
24

Source
2018 Multi-University Training Contest 7
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
#define mod 1000000007
using namespace std;

struct M
{
    ll m[3][3];
    M mul(M o)
    {
        M t;
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        {
            ll temp=0;
            for(int k=0;k<3;k++)
                temp=(temp+m[i][k]*o.m[k][j]%mod)%mod;
            t.m[i][j]=temp;
        }
        return t;
    }
    void zero()
    {
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        m[i][j]=0;
    }
    void unit()
    {
        zero();
        for(int i=0;i<3;i++)m[i][i]=1;
    }
};
M get(M a,int b)
{
    M ans;
    ans.unit();
    while(b)
    {
        if(b&1)ans=ans.mul(a);
        b>>=1;
        a=a.mul(a);
    }
    return ans;
}
int A,B,C,D,P,n;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%d%d%d%d%d%d",&A,&B,&C,&D,&P,&n);
        A%=mod;
        B%=mod;
        if(n==1)cout<<A<<endl;
        else if(n==2)cout<<B<<endl;
        else
        {
            M _a;
            _a.zero();
            _a.m[0][0]=D;_a.m[0][1]=C;_a.m[0][2]=1;
            _a.m[1][0]=1;_a.m[2][2]=1;
            for(ll i=3,last;i<=n;i=last+1)
            {
                ll val=P/i;
                if(val==0)
                {
                    int b=n-i+1;
                    M a=_a;
                    M __a=get(a,b);
                    B=(__a.m[0][0]*B%mod+__a.m[0][1]*A%mod)%mod;
                    //cout<<B<<endl;
                    break;
                }
                last=P/val;
                last=min((int)last,n);
                int b=last-i+1;
                M a=_a;
                M __a=get(a,b);
                ll _B=B;
                B=(__a.m[0][0]*B%mod+__a.m[0][1]*A%mod+__a.m[0][2]*val%mod)%mod;
                __a=get(a,b-1);
                A=(__a.m[0][0]*_B%mod+__a.m[0][1]*A%mod+__a.m[0][2]*(P/(last-1))%mod)%mod;
            }
            printf("%d\n",B);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值