hdu2842---Chinese Rings (matrix)

Source: Internet
Author: User

Problem Description
Dumbear likes to play the Chinese Rings (baguenaudier). It ' s a game played with nine rings on a bar. The rules of this game is very simple:at first, the nine rings is all on the bar.
The first ring can be taken off or taken on with one step.
If The first k rings is all off and the The (k + 1) th ring was on, then the (k + 2) th ring can be taken off or taken on with O Ne step. (0≤k≤7)

Now consider a game with N (n≤1,000,000,000) rings on a bar, Dumbear wants to make all the rings off the bar with least Steps. But Dumbear was very dumb, so he wants your to help him.

Input
Each line of the input file contains a number N indicates the number of the rings on the bar. The last line of the input file contains a number "0".

Output
For each line, the output of an integer S indicates the least steps. For the integers is very large, output S mod 200907.

Sample Input

1 4 0

Sample Output

1 10

Source
Multi-university Training Contest 3-host by WHU

Recommend
Gaojie | We have carefully selected several similar problems for you:2841 2844 2843 2840 2839

If the first k rings are removed and the first k+1 is hung, then the first k+2 can be taken down or mounted.
Set F[n] Indicates the number of steps required to remove the first n rings
It's obvious to take the front n-2 off first: f[n-2]
Take off nth: 1 steps
The rest of the n-1, if you want to dismantle it, then the first n-2 must hang, according to the meaning of the title, need to put the former n-2 hang again, the next is f[n-1]
F[n] = 2 * f[n-2] + f[n-1] + 1
Then the coefficient matrix is introduced, and the matrix can be quickly idempotent

/************************************************************************* > File Name:hdu2842.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 March 12 Thursday 15:42 57 seconds ******************************** ****************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Const intMoD =200907;classmartix{ Public: LL mat[4][4];        Martix (); Martixoperator* (ConstMartix &b)Const; martix&operator= (ConstMartix &b);}; Martix:: Martix () {memset(Mat,0,sizeof(MAT));} Martix Martix::operator* (ConstMartix &b)Const{Martix ret; for(inti =0; I <3; ++i) { for(intj =0; J <3; ++J) { for(intK =0; K <3; ++K) {Ret.mat[i][j] + = ThisMAT[I][K] * B.mat[k][j];            RET.MAT[I][J]%= mod; }        }    }returnRET;} martix& Martix::operator= (ConstMartix &b) { for(inti =0; I <3; ++i) { for(intj =0; J <3; ++J) { ThisMAT[I][J] = B.mat[i][j]; }    }return* This;} Martix Fastpow (Martix A,intN) {Martix ans; ans.mat[0][0] = ans.mat[1][1] = ans.mat[2][2] =1; while(n) {if(N &1) {ans = ans * A; } N >>=1;    A = a * A; }returnAns;}voidDebug (Martix A) { for(inti =0; I <3; ++i) { for(intj =0; J <3; ++J) {printf("%lld", A.mat[i][j]); }printf("\ n"); }}intMain () {intN    Martix A; a.mat[0][0] =1; a.mat[0][1] =1; a.mat[1][0] =2; a.mat[2][0] =1; a.mat[2][2] =1;//Debug (A);     while(~scanf("%d", &n)) {if(!n) { Break; }if(N <3)        {printf("%d\n", n);Continue; } Martix ans = Fastpow (A, N-1);        Martix F; f.mat[0][0] =1; f.mat[0][2] =1; Ans = F * ans;printf("%lld\n", ans.mat[0][0]); }return 0;}

hdu2842---Chinese Rings (matrix)

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.