Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2127 Accepted Submission(s): 836
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;0≤A,B,C,D≤1e9;1≤P,n≤1e9
Sample Input
2
3 3 2 1 3 5
3 2 2 2 1 4
Sample Output
36
24
看题目是矩阵快速幂,但是m=P/i却不是定值,如果m为定值:
ans1 系数矩阵tmp ans2
f2 f1 m D 1 0 f3 f2 m
0 0 0 * C 0 0 = 0 0 0
0 0 0 1 0 1 0 0 0
ans1-->ans2得到系数矩阵,正常情况求得矩阵的结果ans=初始矩阵*系数矩阵^x,但是此题推不出固定公式求ans,每次的与系数相乘的矩阵是不同的,比赛·时想不到解决办法。
下来ctx讲:当m值与前一位不相同的话就得自己将m改为当前正确的m值,再与系数相乘。
m值也不是每次都需要改的,P/i的过程中,值是分段的,因为i是从1到n的,每个m值的最后一个i值是last=p/(p/i),这个推推就知道了。知道了当前相同的m有last-i+1个,同一m就可以用快速幂了。其中还有些细节,见代码吧:
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
using namespace std;
const int maxn=1e5+10;
ll a[maxn];
ll b[maxn];
ll A,B,C,D,p,n;
struct matrix
{
ll m[3][3];
};
matrix mul(matrix a,matrix b)
{
matrix c;
memset(c.m,0,sizeof(c.m));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
c.m[i][j]=(c.m[i][j]+((a.m[i][k]*b.m[k][j])%mod)%mod)%mod;//取模方式注意,因为此wa了都找不到错。
return c;
}
matrix fast(matrix a,ll nn)
{
matrix res;
memset(res.m,0,sizeof(res.m));
//初始化为单位矩阵
res.m[0][0]=1;
res.m[1][1]=1;
res.m[2][2]=1;
while(nn)
{
if(nn&1) res=mul(res,a);
nn>>=1;
a=mul(a,a);
}
return res;
}
int main()
{
int t;
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%mod);
continue;
}
if(n==2)
{
printf("%lld\n",B%mod);
continue;
}
matrix ans,tmp,tmp1;
memset(ans.m,0,sizeof(ans.m));
memset(tmp.m,0,sizeof(tmp.m));
memset(tmp1.m,0,sizeof(tmp1.m));
ans.m[0][0]=B;
ans.m[0][1]=A;
tmp.m[0][0]=D;
tmp.m[0][1]=1;
tmp.m[1][0]=C;
tmp.m[2][0]=1;
tmp.m[2][2]=1;
fast(tmp,1);
int f1=0;
for(int i=3;i<=n;i++)
{
ans.m[0][2]=p/i;
if(i>p)
{
int out=n-i+1;
f1=1;
printf("%lld\n",mul(ans,fast(tmp,out)).m[0][0]);
break;
}
int last=min(p/(p/i),n);//此时m的最后一个i>n的情况可以提前结束了。
tmp1=fast(tmp,last-i+1);
ans=mul(ans,tmp1);
i=last;
}
if(f1==0)printf("%lld\n",ans.m[0][0]);
}
}
听了是这样的分段矩阵快速幂,觉得可以写写,呼呼啦啦写完了,事隔一星期突然想起,又回来开始呼呼啦啦找bug,,终于ac了