CodeForces Round #118 (185A)-Plant

Source: Internet
Author: User

This competition starts...... I wrote A question and went back to bed .. there are many people with three questions in this competition .. this is a typical recursive query .. using Matrix Multiplication...
In addition, N [k] [0] indicates the number of up triangles at the k time... N [k] [1] indicates the number of down triangles at k time... so easy to use:
N [k] [0] = N [k-1] [0] * 3 + N [k-1] [1]
N [k] [1] = N [k-1] [1] * 3 + N [k-1] [0]
Because k is very large, we can only use matrix multiplication to solve the problem... to construct a matrix:
H = [3 1
1 3]
Initial Value: A = {1, 0}
So the k-day condition is required: A * h ^ k ....

Program:
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Include <queue>
# Define OOS 2000000000
# Define ll long
Using namespace std;
Struct node
{
Ll s [2] [2];
} _ 2jie [70], temp;
Ll n;
Node matrix_mul (node a, node B)
{
Int k, I, j;
Memset (temp. s, 0, sizeof (temp. s ));
For (k = 0; k <2; k ++)
For (I = 0; I <2; I ++)
For (j = 0; j <2; j ++)
Temp. s [I] [j] = (temp. s [I] [j] +. s [I] [k] * B. s [k] [j]) % 1000000007;
Return temp;
}
Node getanswer (node h, ll l)
{
Ll k, I;
Node ans;
_ 2jie [1] = h;
K = 2;
For (I = 2; I <63; I ++)
{
_ 2jie [I] = matrix_mul (_ 2jie [I-1], _ 2jie [I-1]);
K * = 2;
}
Ans. s [0] [0] = 1; ans. s [0] [1] = 0;
Ans. s [1] [0] = 0; ans. s [1] [1] = 1;
While (l)
{
While (k> l)
{
I --;
K/= 2;
}
Ans = matrix_mul (ans, _ 2jie [I]);
L-= k;
}
Return ans;
}
Int main ()
{
Scanf ("% I64d", & n );
Node h;
H. s [0] [0] = 3; h. s [0] [1] = 1;
H. s [1] [0] = 1; h. s [1] [1] = 3;
H = getanswer (h, n );
Printf ("% I64d \ n", h.s [0] [0]);
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.