思路: 递推+矩阵快速幂
分析:
1 这一题和hdu 2256
几乎就是一模一样的题目,只是这边要求的是向上取整
那么我们按照hdu2256的思路来做即可点击打开hdu 2256
代码:
/************************************************ * By: chenguolin * * Date: 2013-08-28 * * Address: http://blog.csdn.net/chenguolinblog * ************************************************/ #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long int64; const int N = 2; int a , b , n , MOD; struct Matrix{ int64 mat[N][N]; Matrix operator*(const Matrix& m)const{ Matrix tmp; 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] += mat[i][k]*m.mat[k][j]%MOD; tmp.mat[i][j] %= MOD; } } return tmp; } }; int Pow(Matrix &m){ if(n == 1){ int x = sqrt(b); int ans = a+x; if(x*x != b) ans++; return ans%MOD; } Matrix ans; memset(ans.mat , 0 , sizeof(ans.mat)); for(int i = 0 ; i < N ; i++) ans.mat[i][i] = 1; n--; while(n){ if(n&1) ans = ans*m; n >>= 1; m = m*m; } int64 sum = 0; sum += ans.mat[0][0]*a%MOD; sum += ans.mat[0][1]%MOD; return 2*sum%MOD; } int main(){ Matrix m; while(scanf("%d" , &a) != EOF){ scanf("%d%d%d" , &b , &n , &MOD); m.mat[0][0] = m.mat[1][1] = a; m.mat[0][1] = b ; m.mat[1][0] = 1; printf("%d\n" , Pow(m)); } return 0; }