【HDU 6395】暑期多校day7 Sequence (除法分块、矩阵快速幂)

题目大意

求 Fn ,(n<=1e9)。

F1=AF2=BFn=Fn2C+Fn1D+Pn { F 1 = A F 2 = B F n = F n − 2 ∗ C + F n − 1 ∗ D + ⌊ P n ⌋


解题思路

由于 Pn ⌊ P n ⌋ 的取值只有 sqrt(n) 种情况,所以分段进行矩阵快速幂转移。转移矩阵如下:

lhs=Pn00Fn200Fn100 l h s = [ ⌊ P n ⌋ F n − 2 F n − 1 0 0 0 0 0 0 ]

rhs=1000011CDP/Pn r h s = [ 1 0 1 0 0 C 0 1 D ] P / ⌊ P n ⌋


代码

我居然忘记分块除法怎么写了,生气(啪!)。

#include <bits/stdc++.h>
using namespace std;

const int maxn=int(1e5)+111, moder=int(1e9)+7;
typedef pair<int,int> pii;

inline int add(const int &a,const int &b) {return (a+b<moder)?(a+b):(a+b-moder);}
inline int mul(const int &a,const int &b) {return 1ll*a*b%moder;}

int n,P,A,B,C,D;
vector<pii> v;

struct Mat {
    int a[3][3];
    Mat() {
        for(int i=0;i<3;++i)
        for(int j=0;j<3;++j)
            a[i][j]=0;
    }
    Mat operator * (const Mat &rhs) const {
        Mat R;
        for(int i=0;i<3;++i)
        for(int j=0;j<3;++j) {
            R.a[i][j]=0;
            for(int k=0;k<3;++k)
                R.a[i][j]=add(R.a[i][j],mul(a[i][k],rhs.a[k][j]));
        }
        return R;
    }
}ori, mov;

Mat fpow(Mat a,int k) {
    Mat res;
    for(int i=0;i<3;++i) res.a[i][i]=1;
    for(;k;k>>=1,a=a*a) if(k&1) res=res*a;
    return res;
}

void work() {
    scanf("%d%d%d%d%d%d",&A,&B,&C,&D,&P,&n); n-=2;

    register int i,j;
    for(i=0;i<3;++i)
    for(j=0;j<3;++j)
        ori.a[i][j]=mov.a[i][j]=0;

    ori.a[0][1]=A, ori.a[0][2]=B;
    mov.a[0][0]=mov.a[0][2]=mov.a[2][1]=1, mov.a[1][2]=C, mov.a[2][2]=D;

    int a=3, b=0, cnt=0;
    while(a<=P && n) {
        b=min(P,P/(P/a)), cnt=min(n,b-a+1);
        ori.a[0][0]=P/a;
        ori=ori*fpow(mov,cnt);
        n-=cnt;
        a=b+1;
    }
    if(n) {
        ori.a[0][0]=0;
        ori=ori*fpow(mov,n);
    }
    printf("%d\n",ori.a[0][2]);
    return;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
#endif
    int T;
    for(scanf("%d",&T);T;T--)
        work();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值