HDU-6395 Sequence

Problem Description
Let us define a sequence as below

{ F 1 = A F 2 = B F n = C ∗ F n − 2 + D ∗ F n − 1 + ⌊ p n ⌋ \begin{cases} F_1=A\\ F_2=B\\ F_n=C*F_{n-2}+D*F_{n-1}+\left \lfloor\dfrac{p}{n}\right \rfloor\\ \end{cases} F1=AF2=BFn=CFn2+DFn1+np

Your job is simple, for each task, you should output F n F_n Fn module 1 0 9 + 7 10^9+7 109+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.

1 ≤ T ≤ 20 1≤T≤20 1T20
0 ≤ A , B , C , D ≤ 1 0 9 0≤A,B,C,D≤10^9 0A,B,C,D109
1 ≤ P , n ≤ 1 0 9 1≤P,n≤10^9 1P,n109

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

Sample Output
36
24


(因为第8场做得渣,第7场又近又还勉强拿得出手(毕竟有原题还有板题),所以用这道题证明自己还活着)

如果我们知道如何用矩阵求Fibonacci的话,那么这道题也就比较容易想到矩阵。
因为 ⌊ p n ⌋ \left \lfloor\dfrac{p}{n}\right \rfloor np只有大概 p \sqrt p p 个取值,所以分一下块,就可以用以下的矩阵来解:
( F n F n − 1 1 ) = ( D C ⌊ p n ⌋ 1 0 0 0 0 1 ) × ( F n − 1 F n − 2 1 ) \begin{pmatrix} F_n\\ F_{n-1} \\ 1 \\ \end{pmatrix} = \begin{pmatrix} D & C & \left \lfloor\dfrac{p}{n}\right \rfloor \\ 1 & 0 & 0 \\ 0 & 0 & 1 \\ \end{pmatrix} \times \begin{pmatrix} F_{n-1}\\ F_{n-2} \\ 1 \\ \end{pmatrix} FnFn11=D10C00np01×Fn1Fn21
接下来怎么快速幂就比较显然了。
至于分块什么的比较简单,详见代码。
(不知道矩阵乘法和矩阵快速幂的自行百度)
(p.s.vector会TLE)

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define mod 1000000007
using namespace std;
struct mat
{
    int r,c,a[5][5];
    mat(){};
    mat(int _r,int _c)
    {
        r=_r,c=_c;
        memset(a,0,sizeof(a));
    }
};
mat mul(mat A,mat B)
{
	mat C(A.r,B.c);
	for(int i=0;i<A.r;i++)
		for(int j=0;j<B.c;j++)
			for(int k=0;k<B.r;k++)
				C.a[i][j]=1ll*(C.a[i][j]+1ll*A.a[i][k]*B.a[k][j])%mod;
	return C;
}
mat powmod(mat A,int k)
{
    mat ret(A.r,A.c);
    for(int i=0;i<A.r;i++) ret.a[i][i]=1;
    while(k)
    {
        if(k&1) ret=mul(ret,A);
        A=mul(A,A);
        k>>=1;
    }
    return ret;
}
int t,a,b,c,d,p,n;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&p,&n);
        if(n==1) printf("%d\n",a);
        if(n==2) printf("%d\n",b);
        if(n<=2) continue;
        mat A(3,1),B(3,3);
        A.a[0][0]=b,A.a[1][0]=a,A.a[2][0]=1;
        int last;
        for(int i=3;i<=n;i=last+1)
        {
            if(!(p/i)) last=n;
            else last=min(n,p/(p/i));
            B.a[0][0]=d,B.a[0][1]=c,B.a[0][2]=p/i;
            B.a[1][0]=1,B.a[1][1]=0,B.a[1][2]=0;
            B.a[2][0]=0,B.a[2][1]=0,B.a[2][2]=1;
            B=powmod(B,last-i+1);
            A=mul(B,A);
        }
        printf("%d\n",A.a[0][0]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值