Nyoj 1075 (recursive + rapid matrix power)

Source: Internet
Author: User
Red virus problem time limit: 1000 MS | memory limit: 65535 kb difficulty: 4
Description
Medical researchers have recently discovered a new virus, known as the red virus, because it spreads at a speed comparable to the "red code" that once spread over the Internet ". It was found that, in the DNA sequence of the virus and its variants, both of them are paired. Lyh wants to know the number of all DNA sequences that may become the virus under this feature.
Input
Multiple groups of test data. Enter an integer N for each data set to indicate the length of the virus DNA sequence. (1 ≤ n ≤ 10 ^ 9) If n = 0, the input is over and no processing is required.
Output
Each group of output occupies one row, representing the number of all possible DNA sequences with the virus length of N. Because the result may be very large, you only need to output the result after the remainder of 10007.
Sample Input
12
Sample output
26
Prompt
The DNA sequence is composed of four types of nucleotide, namely, "A", "g", "C", and "T. When n = 2, all possible DNA sequences are TT, TG, GT, GG, AA, and CC.


Analysis: because the question requires that a and c appear in pairs, that is, the numbers of A and C are both even, so four states can be defined:
DP [N] [0] indicates that when the length is N, the number of A is an even number, and the number of C is also an even number,
DP [N] [1] indicates that when the length is N, the number of A is an even number, and the number of C is an odd number,
DP [N] [2] indicates that when the length is N, the number of A is an odd number, and the number of C is an even number,
DP [N] [3] indicates that when the length is N, the number of A is an odd number, and the number of C is also an odd number of DNA sequences,
Then
DP [N] [0] = DP [n-1] [0] * 2 + dp [n-1] [1] * 1 + dp [n-1] [2] * 1 + dp [n-1] [3] * 0
DP [N] [1] = DP [n-1] [0] * 1 + dp [n-1] [1] * 2 + dp [n-1] [2] * 0 + dp [n-1] [3] * 1
DP [N] [2] = DP [n-1] [0] * 1 + dp [n-1] [1] * 0 + dp [n-1] [2] * 2 + dp [n-1] [3] * 1
DP [N] [3] = DP [n-1] [0] * 0 + dp [n-1] [1] * 1 + dp [n-1] [2] * 1 + dp [n-1] [3] * 2
Based on this recursive relationship, a matrix such as the next 4*4 can be constructed,
| 2 1 1 0 |
| 1 2 0 1 |
| 1 0 2 1 |
| 0 1 1 2 |
Then, the answer can be quickly obtained using the matrix's quick power.

# Include <cstdio> # include <cstring> # define mod 10007 struct matrix {int mat [4] [4]; matrix () {memset (MAT, 0, sizeof (MAT); For (INT I = 0; I <4; I ++) mat [I] [I] = 1 ;}}; matrix multi (matrix, matrix B) {matrix res; For (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {res. mat [I] [J] = 0; For (int K = 0; k <4; k ++) {res. mat [I] [J] + =. mat [I] [k] * B. mat [k] [J]; Res. mat [I] [J] % = mod ;}} return res;} matrix POW (matrix A, int X) {matrix res; while (X) {If (X & 1) RES = multi (Res, a); A = multi (a, a); X >>=1;} return res ;} int main () {int t, n; matrix A; // coefficient matrix. mat [0] [0] = 2;. mat [0] [1] = 1;. mat [0] [2] = 1;. mat [0] [3] = 0;. mat [1] [0] = 1;. mat [1] [1] = 2;. mat [1] [2] = 0;. mat [1] [3] = 1;. mat [2] [0] = 1;. mat [2] [1] = 0;. mat [2] [2] = 2;. mat [2] [3] = 1;. mat [3] [0] = 0;. mat [3] [1] = 1;. mat [3] [2] = 1;. mat [3] [3] = 2; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); matrix ans = POW (A, n); printf ("% d \ n", ans. mat [0] [0]);} return 0 ;}


Nyoj 1075 (recursive + rapid matrix power)

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.