题目
思路
很简单的一道数论分块加矩阵加速。
Code:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 5
#define LL long long
#define Mod 1000000007
#define Int register long long
using namespace std;
inline void read(LL &x)
{
x = 0;
LL f = 1;
char s = getchar();
while (s < '0' || s > '9')
{
if (s == '-')
f = -1;
s = getchar();
}
while (s >= '0' && s <= '9')
{
x = (x << 3) + (x << 1) + (s ^ 48);
s = getchar();
}
x *= f;
}
inline LL Max(LL x,LL y)
{
return x > y ? x : y;
}
inline LL Min(LL x,LL y)
{
return x < y ? x : y;
}
struct Matrix
{
LL c[MAXN][MAXN], n, m;
Matrix(){memset(c, 0, sizeof c);}
inline void Clear(){memset(c, 0, sizeof c);}
friend Matrix operator *(Matrix Tx,Matrix Ty)
{
Matrix Res;
Res.n = Tx.n, Res.m = Ty.m;
for (Int i = 1; i <= Tx.n; ++ i)
for (Int k = 1; k <= Tx.m; ++ k)
for (Int j = 1; j <= Ty.m; ++ j)
if (Tx.c[i][k])
Res.c[i][j] += Tx.c[i][k] * Ty.c[k][j] % Mod,
Res.c[i][j] %= Mod;
return Res;
}
}Fuck, Shit;
inline Matrix QuickPow_Matrix(Matrix x,LL y)
{
Matrix Res;
Res.n = Res.m = x.m;
for (Int i = 1; i <= Res.n; ++ i)
Res.c[i][i] = 1;
while ( y )
{
if (y & 1)
Res = Res * x;
x = x * x;
y /= 2;
}
return Res;
}
LL A, B, C, D, P, n;
int main()
{
LL T;
read( T );
while (T --)
{
Fuck.Clear();
read( A ); read( B ); read( C );
read( D ); read( P ); read( n );
Fuck.n = 1, Fuck.m = 3;
Fuck.c[1][1] = B, Fuck.c[1][2] = A, Fuck.c[1][3] = 1;
for (Int l = 3, r; l <= n; l = r + 1)
{
Shit.Clear();
LL t = P / l;
if (! t)
r = n;
else r = Min(n, P / t);
Shit.n = Shit.m = 3;
Shit.c[1][1] = D, Shit.c[2][1] = C, Shit.c[3][1] = t;
Shit.c[1][2] = Shit.c[3][3] = 1;
Shit = QuickPow_Matrix(Shit, r - l + 1);
Fuck = Fuck * Shit;
}
printf("%lld\n", Fuck.c[1][1]);
}
return 0;
}