FZU-1683 commemorating the SlingShot matrix power

Source: Internet
Author: User

FZU-1683 commemorating the SlingShot matrix power

Known F (n) = 3 * F (n-1) + 2 * F (n-2) + 7 * F (n-3), n> = 3, where F (0) = 1, F (1) = 3, F (2) = 5, for each given n, the output F (0) + F (1) + ...... + F (n) mod 2009.

Solution: Borrow others' pictures

This question is similar to HDU-1757 A Simple Math Problem, but this question has many similarities. In fact, the idea is similar. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> #include #include #define mod 2009const int N = 4;typedef long long ll;struct Matrix{ ll mat[N][N];}a, b, tmp;int n;void init() { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) a.mat[i][j] = b.mat[i][j] = 0; for(int i = 0; i < N; i++) b.mat[i][i] = 1; a.mat[0][0] = a.mat[0][1] = a.mat[2][1] = a.mat[3][2] = 1; a.mat[1][1] = 3; a.mat[1][2] = 2; a.mat[1][3] = 7;}Matrix matrixMul(Matrix x, Matrix y) { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) { tmp.mat[i][j] = 0; for(int k = 0; k < N; k++) tmp.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % mod; } return tmp;}void solve() { while(n) { if(n & 1) b = matrixMul(b,a); a = matrixMul(a,a); n >>= 1; }}int main() { int test, cas = 1; scanf("%d", &test); while(test--) { scanf("%d", &n); init(); if(n <= 2) { switch(n) { case 0:printf("Case %d: 1\n", cas++);break; case 1:printf("Case %d: 4\n");break; case 2:printf("Case %d: 9\n");break; } continue; } n -= 1; solve(); printf("Case %d: %lld\n",cas++, (b.mat[0][0] * 4 + b.mat[0][1] * 5 + b.mat[0][2] * 3 + b.mat[0][3] * 1) % 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.