二分法矩阵求斐波那契(fibonacci)数列第n项

http://blog.csdn.net/kz10211003/article/details/6871301

 

 

void multiply(long a[][], long b[][], int MOD) {
long c[][] = new long[2][2];
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % MOD) % MOD;
}
}
}
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
a[i][j] = c[i][j];
}

void pow(long a[][], long n, int MOD) {
long ret[][] = new long[2][2];
ret[0][0] = ret[1][1] = 1;
while (n > 0) {
if ((n & 1) > 0) multiply(ret, a, MOD);
n >>= 1;
multiply(a, a, MOD);
}
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
a[i][j] = ret[i][j];
}

 

UVA 10518

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;

public classMain {
	
	void multiply(long a[][], long b[][], int MOD) {
		long c[][] = new long[3][3];
		for (int i = 0; i < 3; ++i) {
			for (int j = 0; j < 3; ++j) {
				for (int k = 0; k < 3; ++k) {
					c[i][j] = (c[i][j]  + (a[i][k] * b[k][j]) % MOD) % MOD;
				}
			}
		}
		for (int i = 0; i < 3; ++i) 
			for (int j = 0; j < 3; ++j) 
				a[i][j] = c[i][j];
	}
	
	void pow(long a[][], long n, int MOD) {
		long ret[][] = new long[3][3];
		ret[0][0] = ret[1][1] = ret[2][2] = 1;
		while (n > 0) {
			if ((n & 1) > 0) multiply(ret, a, MOD);
			n >>= 1;
			multiply(a, a, MOD);
		}
		for (int i = 0; i < 3; ++i) 
			for (int j = 0; j < 3; ++j) 
				a[i][j] = ret[i][j];
	}
	
	long n;
	int b;
	
	void run() throws Exception {
		int Case = 0;
		while (true) {
			n = cin.nextLong(); b = cin.nextInt();
			if (0 == n && 0 == b) break;
			if (n < 2) {
				out.println("Case " + ++Case + ": " + n + " " + b + " " + (1 % b));
			} else {
				long ar[][] = new long[3][3];
				ar[0][0] = ar[0][1] = ar[0][2] = ar[1][0] = ar[2][2] = 1;
				pow(ar, n - 1, b);
				out.println("Case " + ++Case + ": " + n + " " + b + " " + (ar[0][0] + ar[0][1] + ar[0][2]) % b);
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
        new Main().run();	
        out.close();
	}
	
	static Scannercin = new Scanner(new BufferedInputStream(System.in));
	static PrintWriterout = new PrintWriter(System.out);
//	static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));                                                                                                                                                                                                                                                                                              zbb                                                                                                                                                                                                                                                                                                                                                                                                                   v    
}

posted on 2013-04-19 15:16  Sure_Yi  阅读(306)  评论(0编辑  收藏  举报

导航