題目大意:求
解題思路:這題跟HDU - 2256 Problem of Precision類似,只不過這題是向上取整
有一個隱藏的條件:(a-1)^2 < b < a ^ 2
表明a - 1 < b < a
也就是(a - sqrt(b) )^n是小於1的
注意int的范圍,有可能會溢出
#include
typedef long long ll;
const int N = 2;
struct Matrix{
ll mat[N][N];
}A, B, tmp;
int a, b, n, m;
void init() {
B.mat[0][0] = B.mat[1][1] = 1;
B.mat[0][1] = B.mat[1][0] = 0;
A.mat[0][0] = A.mat[1][1] = a;
A.mat[0][1] = b;
A.mat[1][0] = 1;
}
Matrix matrixMul(Matrix x, Matrix y) {
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++) {
tmp.mat[i][j] = 0;
for(int k = 0; k < N; k++)
tmp.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % m;
}
return tmp;
}
void solve(){
while(n) {
if(n & 1)
B = matrixMul(B,A);
A = matrixMul(A,A);
n >>= 1;
}
}
int main() {
while(scanf("%d%d%d%d", &a, &b, &n, &m) != EOF) {
init();
solve();
printf("%I64d\n", (2 * B.mat[0][0]) % m);
}
return 0;
}