Hdu 2842 (Rapid matrix power + recursion)
A Chinese ring game has n rings on a wooden stick. The first ring can be placed or removed at will, the rest of the ring x if you want to put or remove must be the first ring X-1 is placed and the first X-2 ring is all removed, ask n rings at least how many operations can be all removed.
Problem: recurrence is required. First, the first step must be to remove the nth Environmental Protection Certificate with the least number of operations, because whether the subsequent ring exists will not affect the previous ring, however, if you want to remove the first ring, you need to put the first ring. f (n) indicates the minimum number of operations required to remove the first n rings, first split the n to remove the first N-2 and then split the n, spend f (n-2) + 1, then it is 00... 0010, to remove the n-1 need to put the first N-2, the number of steps spent and remove is the same as f (n-2), then is 11... 1110, all removed is f (n-1), so the recursive formula is f (n) = f (n-1) + 2 * f (n-2) + 1. The final matrix is a fast power.
#include
#include
const int MOD = 200907;struct Mat { long long g[3][3];}ori, res;long long n;Mat multiply(Mat x, Mat y) { Mat temp; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { temp.g[i][j] = 0; for (int k = 0; k < 3; k++) temp.g[i][j] = (temp.g[i][j] + x.g[i][k] * y.g[k][j]) % MOD; } return temp;}void calc(long long n) { while (n) { if (n & 1) ori = multiply(ori, res); n >>= 1; res = multiply(res, res); }}int main() { while (scanf("%lld", &n) == 1 && n) { if (n == 1 || n == 2) { printf("%lld\n", n); continue; } memset(ori.g, 0, sizeof(ori.g)); memset(res.g, 0, sizeof(res.g)); ori.g[0][0] = 2; ori.g[0][1] = ori.g[0][2] = 1; res.g[0][0] = res.g[0][1] = res.g[2][0] = res.g[2][2] = 1; res.g[1][0] = 2; calc(n - 2); printf("%lld\n", ori.g[0][0]); } return 0;}