11885-Number of battlefields
Question: calculate the number of battlefields that can enclose the perimeter, excluding rectangles.
Idea: The specific recursion is not recursive, but I read a rule on the Internet. If the answer to the rectangle is a Fibonacci sequence (but the odd number is 0), then the number of rectangles is the answer, the number of rectangles is n/2-1, which can be obtained by using the matrix power.
Which of the following is the specific recursive process...
Code:
#include <stdio.h>#include <string.h>const long long MOD = 987654321;int p;struct mat {long long v[2][2];mat() {memset(v, 0, sizeof(v));}mat operator *(mat c) {mat ans;for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {for (int k = 0; k < 2; k++) {ans.v[i][j] = (ans.v[i][j] + v[i][k] * c.v[k][j] % MOD) % MOD; } } } return ans; } mat operator^(int k) { mat x = *this, ans; ans.v[0][0] = ans.v[1][1] = 1; while (k) { if (k&1) ans = ans * x;x = x * x;k >>= 1; } return ans; }};int main() {while (~scanf("%d", &p) && p) {if(p % 2) {printf("0\n"); continue;}mat pri;pri.v[0][0] = pri.v[0][1] = pri.v[1][0] = 1;pri = pri^(p - 4);printf("%lld\n",(pri.v[0][0] - ((long long)p / 2 - 1) + MOD) % MOD); }return 0;}