给定递推式:
求Fn.
分析:给出的公式可以用快速矩阵幂运算得到,但 P/n 整除对于不同的i,值是不同的。
可以根据P将3-n分成若干块,每块中P整除n的值是相同的。分块的时候要注意判断。
将每块的快速幂结果累乘得到结果。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MAXN = 1e5+;
const int SIZE = ;
const int MOD = 1e9+;
int N;
int A, B, C, D, P;
int cc; LL ret_multi[SIZE][SIZE];
void multi(LL a[][SIZE], LL b[][SIZE])
{
memset(ret_multi, , sizeof ret_multi);
for( int i = ; i < SIZE; i++ )
for( int j = ; j < SIZE; j++)
for( int k = ; k < SIZE; k++ )
ret_multi[i][j] = (ret_multi[i][j] + a[i][k] *b[k][j]) %MOD;
for( int i = ; i < SIZE; i++ )
for( int j = ; j < SIZE; j++)
a[i][j] = ret_multi[i][j];
} LL ret_qpow[SIZE][SIZE];
LL base[SIZE][SIZE];
void qpow(LL b[][SIZE], LL index)
{
memset(ret_qpow, , sizeof ret_qpow);
memcpy(base, b, sizeof base); for( int i = ; i < SIZE; i++) ret_qpow[i][i] = ;
while(index){
if( index &)
multi(ret_qpow, base);
index /= ;
multi(base, base);
}
} LL m[][SIZE] = {
{D, C, cc},
{, , },
{, , }
}; LL f[][SIZE] = {
{B, , },
{A, , },
{, , }
}; int getRight(int cc)
{
int L = , R = N;
int ret = N;
while(L <= R){
int mid = L + (R-L)/;
if( P/mid >= cc){
ret = mid;
L = mid+;
}
else R = mid-;
}
return ret;
} vector<PII> range; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif int T; scanf("%d",&T);
while(T--){
range.clear();
scanf("%d%d%d%d%d%d",&A,&B,&C,&D,&P,&N);
if(N==){
printf("%d\n",A); continue;
}
else if(N==){
printf("%d\n",&B); continue;
}
for( int i = ; i <= N; i++ ) //分块
{
int cc = P/i;
int j = getRight(cc);
range.push_back({i, j});
i = j;
} LL m[][SIZE] = {
{D, C, cc},
{, , },
{, , }
}; LL f[][SIZE] = {
{B, , },
{A, , },
{, , }
}; for( PII rng :range){
const int &n1 = rng.first;
const int &n2 = rng.second;
cc = P/n1; m[][] = cc; qpow(m, n2-(n1-));
multi(ret_qpow, f); memcpy(f, ret_multi, sizeof f);
}
printf("%lld\n",f[][]%MOD);
}
return ;
}