hdu6395 2018 hdu 多校第七场 Sequence

Problem Description

Let us define a sequence as below
 

F1=A

F2=B

Fn=C*Fn−2+D*Fn−1+⌊P/n⌋

  Your job is simple, for each task, you should output Fn module 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≤200≤A,B,C,D≤1091≤P,n≤109

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

Recommend

chendu

题意:给出ABCDPn,根据表达式求fn,

思路:用了分块+矩阵快速幂。

因为T<=20,所以n<=100005时,可以直接暴力循环求,大于时,前100000要循环求,其他的根据P/i的值分块,一开始前100000没有算,直接分的所有块,导致前面的项分的块太多,P/i变的太频繁,但是100000后面就是根号n的块,所以要先处理前面不能分块的部分,分块的循环是枚举的P/i的值。

然后可以写系数矩阵,进而转化成矩阵的公式,用快速幂模板就可以。  大佬做的。。。

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 3
#define mod 1000000007
 
ll A,B,C,D,P,n;
 
struct Matrix
{
    Matrix(){memset(a,0,sizeof(a));}
    ll a[M][M];
    void init()
    {
        for(ll i=0;i<M;i++)
            for(ll j=0;j<M;j++)
                a[i][j]=0;
        for(ll i=0;i<M;i++)
            a[i][i]=1;
    }
}MA,MT;
Matrix mul(Matrix a,Matrix b) //(a*b)%mod
{
    Matrix ans;
    ll i,j,k;
    for(i=0;i<M;i++)
        for(j=0;j<M;j++)
    {
        for(k=0;k<M;k++)
            ans.a[i][j]+=a.a[i][k]*b.a[k][j];
        ans.a[i][j]%=mod;
    }
    return ans;
}
Matrix pow(Matrix a,ll n) //(a^n)%mod
{
    Matrix ans;
    ans.init();
    while(n)
    {
        if(n&1)
            ans=mul(ans,a);
        n>>=1;
        a=mul(a,a);
    }
    return ans;
}
 
int main()
{
    int T;
    ll n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld%lld%lld%lld%lld",&A,&B,&C,&D,&P,&n);
 
        if(n==1)
            printf("%lld\n",&A);
        else if(n==2)
            printf("%lld\n",&B);
        else if(n==3)
            printf("%lld\n",(C*A+D*B+P/3)%mod);
        else if(n<=100005)
        {
            ll x,y,z;
            x=A;
            y=B;
            for(int i=3;i<=n;i++)
            {
                z=C*x+D*y+P/i;
                z%=mod;
                x=y;
                y=z;
 
            }
            printf("%lld\n",y);
        }
        //else printf("\n");
 
        else
        {
            ll x,y,z;
            x=A;
            y=B;
            for(int i=3;i<=100000;i++)
            {
                z=C*x+D*y+P/i;
                z%=mod;
                x=y;
                y=z;
 
            }
 
            MT.a[0][0]=1; MT.a[0][1]=0; MT.a[0][2]=0;
            MT.a[1][0]=0; MT.a[1][1]=1; MT.a[1][2]=0;
            MT.a[2][0]=0; MT.a[2][1]=0; MT.a[2][2]=1;
 
            int l=n,r,c;
            for(int i=P/n;i<=P/100001;i++)
            {
                r=P/(i+1)+1;
                r=max(r,100001);
                c=l-r+1;
 
                MA.a[0][0]=D; MA.a[0][1]=C; MA.a[0][2]=i;
                MA.a[1][0]=1; MA.a[1][1]=0; MA.a[1][2]=0;
                MA.a[2][0]=0; MA.a[2][1]=0; MA.a[2][2]=1;
 
                MA=pow(MA,c);
                MT=mul(MT,MA);
                l=r-1;
            }
 
            ll ans=(MT.a[0][0]*y+MT.a[0][1]*x+MT.a[0][2]*1)%mod;
 
            printf("%lld\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37748451/article/details/81637141