SequenceTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1407 Accepted Submission(s): 512 Problem Description Let us define a sequence as below⎧⎩⎨⎪⎪⎪⎪⎪⎪F1F2Fn===ABC⋅Fn−2+D⋅Fn−1+⌊Pn⌋ 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.
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 | We have carefully selected several similar problems for you: 6396 6395 6394 6393 6392 |
根据递推式求第n项,线性递推考虑矩阵快速幂,因为递推式中有向下取整,所以可以把n项拆成一段段的,每一段中的常数项相同,就可以分段矩阵快速幂,二分一直有些迷,膜了大佬之后才明白了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll A,B,C,D,P,N,T,f1,f2;
struct matrix
{
ll a[3][3];
matrix()
{
memset(a,0,sizeof(a));
}
matrix operator*(matrix &p)
{
matrix ans;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
ans.a[i][j]=(ans.a[i][j]+a[i][k]*p.a[k][j]);
ans.a[i][j]%=mod;
}
}
return ans;
}
}U,X;
matrix quick_pow(matrix A,int b)
{
matrix ans=U;
while(b)
{
//printf("111\n");
if(b&1) ans=ans*A;
A=A*A;
b>>=1;
}
return ans;
}
ll srch(ll i)
{
ll key=P/i;
ll l=i,r=N;
while(l<r)
{
int mid=r-(r-l)/2;
if(P/mid==key) l=mid;
else if(P/mid<key)
{
r=mid-1;
}
else l=mid+1;
}
return l;
}
int main()
{
U.a[0][0]=U.a[1][1]=U.a[2][2]=1;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld%lld%lld%lld",&A,&B,&C,&D,&P,&N);
if(N==1)
{
printf("%lld\n",A);
continue;
}
if(N==2)
{
printf("%lld\n",B);
continue;
}
memset(X.a,0,sizeof(X.a));
X.a[0][0]=D;
X.a[0][1]=1;
X.a[1][0]=C;
X.a[2][2]=1;
f1=B%mod;
f2=A%mod;
ll i=3;
while(i<=N)
{
//printf("i=%lld\n",i);
ll j=srch(i);
X.a[2][0]=P/i;
matrix ans=quick_pow(X,j-i+1);
ll f11=(f1*ans.a[0][0]%mod+f2*ans.a[1][0]%mod+ans.a[2][0])%mod;
ll f22=(f1*ans.a[0][1]+f2*ans.a[1][1]+ans.a[2][1])%mod;
f1=f11;
f2=f22;
i=j+1;
//printf("i=%lld %lld %lld\n",i,j,N);
}
printf("%lld\n",f1);
}
return 0;
}