Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

其实这题矩阵都只需要算一半的,看较短代码

Posted by JackyZ at 2010-07-26 11:45:54 on Problem 3070
#include <iostream>
#define mod 10000

int fibonacci(int n) {
	int ra,rb,a,b,x,y;
	if (n<2) return n;
	ra=rb=a=b=1;
	n-=2;
	while (n) {
		if (n&1) {
			x=ra*a+rb*b;
			y=ra*b+rb*(a-b+mod);
			ra=x%mod;rb=y%mod;
		}
		n>>=1;
		x=a*a+b*b;
		y=(2*a+mod)*b-b*b;
		a=x%mod;b=y%mod;
	}
	return ra;
}

int main() {
	for (int n;scanf("%d",&n),n!=-1;) {
		printf("%d\n",fibonacci(n));
	} return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator