Poj3519 Lucky Coins Sequence matrix quick power

Source: Internet
Author: User

Lucky Coins Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 608 Accepted Submission (s): 319

 

Problem Description
As we all know, every coin has two sides, with one side facing up and another side facing down. now, We consider two coins's state is same if they both facing up or down. if we have N coins and put them in a line, all of us know that it will be 2 ^ N different ways. we call a "N coins sequence" as a Lucky Coins Sequence only if there exists more than two continuous coins's state are same. how many different Lucky Coins Sequences exist?


Input
There will be sevaral test cases. For each test case, the first line is only a positive integer n, which means n coins put in a line. Also, n not exceed 10 ^ 9.


Output
You shoshould output the ways of lucky coins sequences exist with n coins, but the answer will be very large, so you just output the answer module 10007.


Sample Input
3
4

Sample Output
2
6

Source
2010 ACM-ICPC Multi-University Training Contest (9) -- Host by HNU
By analyzing the question meaning, we can find that it is dp. How can we find the recursive formula? Dp [I] [j] indicates that there is an I-bit length, and the last few j-bit are connected!
Dp [I] [3] = dp [I-1] [2];

Dp [I] [2] = dp [I-1] [1];

Dp [I] [1] = dp [I-1] [1] + dp [I-1] [2];

Dp [1] [1] = 2; dp [1] [2] = 0; dp [1] [3] = 0;

In this way, we can convert it into a matrix summation!

 

Include <iostream> # include <stdio. h> # include <string. h> using namespace std; # define mod 10007 struct node {int m [4] [4]; node operator * (node B) const // overload multiplication {int I, j, k; node c; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) {c. m [I] [j] = 0; for (k = 0; k <4; k ++) {c. m [I] [j] + = m [I] [k] * B. m [k] [j]; c. m [I] [j] % = mod; // both require modulo} return c ;}; node original, result; void quickm (int n) {node, b; B = original; a = result; while (n) {If (n & 1) {B = B * a;} n = n> 1; a = a * a;} printf ("% d \ n ", 2 * B. m [0] [3] % mod);} int main () {int I, j, n; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) {original. m [I] [j] = (I = j )? 1:0; // initialized as a matrix of units} memset (result. m, 0, sizeof (result. m); result. m [0] [0] = result. m [0] [1] = result. m [1] [0] = result. m [1] [2] = result. m [2] [3] = 1; result. m [3] [3] = 2; while (scanf ("% d", & n )! = EOF) {quickm (n);} 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.