矩阵快速幂
转移矩阵很容易看出来,关键是p/i怎么处理。。
其实是有规律的。。第i项的p/i是x,那么第p / (p / i)项也是x。。且中间全是x。。
然后分段转移就行了
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int MOD = 1e9 + 7;
ll a, b, c, d, p, n, _;
struct Matrix{
ll m[3][3];
Matrix(){
full(m, 0);
}
};
Matrix e;
Matrix mul(Matrix &p, Matrix &q){
Matrix ret;
for(int i = 0; i < 3; i ++){
for(int j = 0; j < 3; j ++){
for(int k = 0; k < 3; k ++){
ret.m[i][j] = (ret.m[i][j] % MOD + p.m[i][k] * q.m[k][j] % MOD) % MOD;
}
}
}
return ret;
}
Matrix fpow(Matrix &x, ll p){
Matrix ret = e;
for(; p; p >>= 1, x = mul(x, x)){
if(p & 1) ret = mul(ret, x);
}
return ret;
}
int main(){
FAST_IO;
for(cin >> _; _; _ --){
cin >> a >> b >> c >> d >> p >> n;
e.m[0][0] = 1, e.m[1][1] = 1, e.m[2][2] = 1;
if(n == 1) printf("%lld
", a % MOD);
else if(n == 2) printf("%lld
", b % MOD);
else{
bool good = false;
Matrix tmp;
tmp.m[0][0] = d, tmp.m[0][1] = c, tmp.m[1][0] = 1, tmp.m[2][2] = 1;
for(int i = 3; i <= n;){
if(p / i == 0){
Matrix t = tmp;
t = fpow(t, n - i + 1);
ll ans = (t.m[0][0] * b % MOD + t.m[0][1] * a % MOD + t.m[0][2]) % MOD;
printf("%lld
", ans);
good = true;
break;
}
else{
Matrix t = tmp;
t.m[0][2] = p / i;
int j = min(n, p / (p / i));
t = fpow(t, j - i + 1);
ll nb = (t.m[0][0] * b % MOD + t.m[0][1] * a % MOD + t.m[0][2]) % MOD;
ll na = (t.m[1][0] * b % MOD + t.m[1][1] * a % MOD + t.m[1][2]) % MOD;
a = na, b = nb;
i = j + 1;
}
}
if(!good) printf("%lld
", b % MOD);
}
}
return 0;
}