#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; }
UVA 11885 - Number of Battlefields(斐波那契),布布扣,bubuko.com
UVA 11885 - Number of Battlefields(斐波那契)
原文地址:http://blog.csdn.net/accelerator_/article/details/36235163