本文共 1601 字,大约阅读时间需要 5 分钟。
这题比较神,不看题解我是想不出来T_T
证明过程如下
之后直接无脑快速幂就好了。。
#include #include #include #include #include #include #include #include #include #include using namespace std; #define MP make_pair#define PB push_backtypedef long long LL;typedef unsigned long long ULL;typedef vector VI;typedef pair PII;typedef pair PDD;const int INF = INT_MAX / 3;const double eps = 1e-8;const LL LINF = 1e17;const double DINF = 1e60;const int maxn = 5;LL n, mod; struct Matrix { int n, m; LL data[maxn][maxn]; Matrix(int n = 0, int m = 0): n(n), m(m) { memset(data, 0, sizeof(data)); } void print() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cout << data[i][j] << " "; } cout << endl; } }}; Matrix operator * (Matrix a, Matrix b) { int n = a.n, m = b.m; Matrix ret(n, m); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { for(int k = 1; k <= a.m; k++) { ret.data[i][j] += a.data[i][k] * b.data[k][j]; ret.data[i][j] %= mod; } } } return ret;} Matrix pow(Matrix mat, LL k) { if(k == 0) { Matrix ret(mat.n, mat.m); for(int i = 1; i <= mat.n; i++) ret.data[i][i] = i; return ret; } if(k == 1) return mat; Matrix ret = pow(mat * mat, k / 2); if(k & 1) ret = ret * mat; return ret;} int main() { int T; cin >> T; while(T--) { cin >> n >> mod; Matrix A0(2, 1), A(2, 2); A0.data[1][1] = 1; A0.data[2][1] = 0; A.data[1][1] = A.data[1][2] = A.data[2][1] = 1; A.data[2][2] = 0; A0 = pow(A, 2 * n) * A0; cout << A0.data[2][1] << endl; } return 0;}
转载于:https://www.cnblogs.com/rolight/p/4056066.html