# 剑指 Offer 10- II. 青蛙跳台阶问题

0 <= n <= 100

class Solution {
public:
int numWays(int n) {
if(n == 0) return 1;
if(n == 1) return 1;
if(n == 2) return 2;
int a = 1, b = 2;
for(int i = 3; i <= n; ++i) {
int tmp = (a + b) % 1000000007;
a = b;
b = tmp;
}
return b;
}
};

# 剑指 Offer 10- I. 斐波那契数列

F(0) = 0,   F(1) = 1
F(N) = F(N – 1) + F(N – 2), 其中 N > 1.

0 <= n <= 100

class Solution {
public:
int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
long long a = 0, b = 1;
for(int i = 2; i <= n; ++i) {
long long tmp = a + b;
tmp %= 1000000007;
a = b;
b = tmp;
}
return b;
}
};

typedef long long LL;

class Solution {
private:
vector<vector<LL>> multiply(vector<vector<LL>> &m1, vector<vector<LL>> &m2) {
int n = m1.size();
vector<vector<LL>> ans(n, vector<LL>(n, 0));
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
for(int k = 0; k < n; ++k) {
ans[i][j] += m1[i][k] * m2[k][j] % 1000000007;
}
}
}
return ans;
}
public:
int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;

vector<vector<LL>> matrix = {{1,1},{1,0}};

vector<vector<LL>> ans = {{1,0},{0,1}};
while(n != 0) {
if(n % 2 == 1) ans = multiply(ans, matrix);
matrix = multiply(matrix, matrix);
n >>= 1;
}

return ans % 1000000007;
}
};

# POJ 3070-Fibonacci

POJ 3070-Fibonacci

Fibonacci

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 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 Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn 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 two 2 × 2 matrices is given by

.

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

.

SourceStanford Local 2006

$$\begin{equation}f(n)=\begin{cases} 0, & n=0;\ 1, & n=1;\ f(n-1)+f(n-2), & n>1.\end{cases}\end{equation}$$

typedef long long LL;
LL Fib1(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return Fib1(n – 1) + Fib1(n – 2);
}

这种算法的时间复杂度是 $O(2^n)$。 但是这种解法有很多重复计算，比如算f(8)=f(7)+f(6)时，f(7)重复计算了f(6)。所以我们可以从小开始算起，把f(n)的前两个值保存下来，每次滚动赋值，代码如下：

LL Fib2(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
LL fn_2 = 0, fn_1 = 1, ans = 0;
for (int i = 2; i <= n; ++i) {
ans = fn_1 + fn_2;
fn_2 = fn_1;
fn_1 = ans;
}
return ans;
}

const int MAXN = 2;
vector<vector<LL> > multi(const vector<vector<LL> >& mat1, const vector<vector<LL> >& mat2)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i) {
for (int j = 0; j < MAXN; ++j) {
for (int k = 0; k < MAXN; ++k) {
mat[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
return mat;
}
vector<vector<LL> > fastPow(vector<vector<LL> >& mat1, int n)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i)
mat[i][i] = 1;
while (n != 0) {
if (n & 1)
mat = multi(mat, mat1);
mat1 = multi(mat1, mat1);
n >>= 1;
}
return mat;
}
LL Fib3(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
vector<vector<LL> > mat = { { 1, 1 }, { 1, 0 } };
vector<vector<LL> > matN = fastPow(mat, n);
return matN;
}

POJ这题的代码如下，古老的POJ居然不支持第37行的vector初始化，崩溃。

#include <iostream> #include<cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 2;
const int MOD = 10000;
vector<vector<LL> > multi(const vector<vector<LL> >& mat1, const vector<vector<LL> >& mat2)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i) {
for (int j = 0; j < MAXN; ++j) {
for (int k = 0; k < MAXN; ++k) {
mat[i][j] = (mat[i][j] + mat1[i][k] * mat2[k][j]) % MOD;
}
}
}
return mat;
}
vector<vector<LL> > fastPow(vector<vector<LL> >& mat1, int n)
{
vector<vector<LL> > mat(MAXN, vector<LL>(MAXN, 0));
for (int i = 0; i < MAXN; ++i)
mat[i][i] = 1;
while (n != 0) {
if (n & 1)
mat = multi(mat, mat1);
mat1 = multi(mat1, mat1);
n >>= 1;
}
return mat;
}
LL Fib3(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
//vector<vector<LL>> mat = { { 1,1 },{ 1,0 } }; //———————–
vector<vector<LL> > mat;
vector<LL> r1;
r1.push_back(1), r1.push_back(1);
vector<LL> r2;
r2.push_back(1), r2.push_back(0);
mat.push_back(r1);
mat.push_back(r2); //———————-
vector<vector<LL> > matN = fastPow(mat, n);
return matN;
}
int main()
{
int n;
while (scanf("%d", &n) && n != -1) {
LL ans = Fib3(n);
printf("%d\n", ans % MOD);
}
return 0;
}

# LeetCode Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps


Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

class Solution {
public:
int climbStairs(int n)
{
if (n == 1)
return 1;
if (n == 2)
return 2;
return climbStairs(n – 1) + climbStairs(n – 2);
}
};

class Solution {
public:
int climbStairs(int n)
{
if (n == 1)
return 1;
if (n == 2)
return 2;
int a1 = 1, a2 = 2, tmp;
for (int i = 3; i <= n; i++) {
tmp = a1 + a2;
swap(a1, a2);
swap(a2, tmp);
}
return a2;
}
};