版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/81637927
Problem Description
Let us define a sequence as below
Your job is simple, for each task, you should output Fn module
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.
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;
}