Matrix Quick Power Template

Source: Internet
Author: User
Tags mul

Description

In the Fibonacci integer sequence, f_0 = 0, f_1 = 1, and f_n = f_{n-1} + f_{n-2} for n \geq 2. For example, the first ten terms of the Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, \cdots

An alternative formula for the Fibonacci sequence is

Given an integer n, your goal was to compute the last 4 digits of f_n.

Input

The input test file would contain multiple test cases. Each test case consists of a containing n (where 0 \leq n \leq 1,000,000,000).

The end-of-file is denoted by a single line containing the number -1 .

Output

For each test case, print the last four digits of f_n. If the last four digits of f_n is all zeros, print 0 ; otherwise, omit any leading zeros (i.e., print f_n< c6> mod 10000).

Sample Input

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875

Hint

As a reminder, matrix multiplication is associative, and the product of the 2 \times 2 matrices are givenby

Also, note that raising any 2 \times 2 matrix to the 0th power gives the identity matrix:

The data used in this problem are unofficial data prepared by 695375900. So any mistake this does not imply mistake in the offcial judge data.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <sstream>7#include <string>8 using namespacestd;9 #defineM 10000Ten structMatrix One { A     inta[2][2]; - }; - Matrix Mul (Matrix X,matrix y) the { - Matrix temp; -memset (TEMP.A,0,sizeof(TEMP.A)); -      for(intI=0;i<2; i++) +          for(intj=0;j<2; j + +) -          for(intk=0;k<2; k++) +Temp.a[i][j]= (Temp.a[i][j]+x.a[i][k]*y.a[k][j])%M; A     returntemp; at } -Matrix Mpow (Matrix A,intN) - { - matrix B; -memset (B.A,0,sizeof(B.A)); -      for(intI=0;i<2; i++) inb.a[i][i]=1; -      while(n>0) to     { +         if(n&1) -b=Mul (b,a); theA=Mul (a,a); *n>>=1; $     }Panax Notoginseng     returnB; - } the intMain () + { A matrix A; the    intN; +     while(~SCANF ("%d",&N)) -    { $a.a[0][0]=1; a.a[0][1]=1; $a.a[1][0]=1; a.a[1][1]=0; -     if(n==-1) -     Break; the     if(n==0) -{printf ("0\n");Wuyi    Continue;} the    if(n==1) -{printf ("1\n"); Wu     Continue;} -  AboutA=Mpow (a,n); $printf"%d\n", a.a[1][0]);} -     return 0; -}

Matrix Quick Power Template

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.