Va 11885-Number of battlefields (Fibonacci)

Source: Internet
Author: User
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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.