High Precision Number staircase

High Precision Number staircase

topic

Title Description

Order N stairs, the upper step can step up the stairs, a step may be the second order.

Compile a program that calculates the total number of different moves.

Input Format

A number that staircase.

Output Format

Several ways to go.

Sample input and output

Input # 1 replication
4
outputs # 1 replication
5
Description / Tips
60% N <= 50
100% N <= 5000)

analysis

Originally a look, it is not yet Fibonacci number, go directly finished formula, (dp [i] = dp [i-1] + dp [i-2];)
results timeout lot. . . . . In fact, this question mark (precision) that there are so buttoned. . .

//int n;
//int dp[5005];
//int main(){
//	cin>>n;
//	dp[0]= 1;
//	dp[1]= 1;
//	for(int i=2;i<=n;i++){
//		dp[i] = dp[i-1]+ dp[i-2];
//	}
//	cout<<dp[n]<<endl;
//	return 0;
//}

Made from that number deed Fibonacci columns, c (i) = c ( i-1) + c (i-2); Here we c (i-1) as a, c (i-2) as b, c ( i) as c.
For example: a is a first stage, a = 1 (one kind of moves), b is a second order, b = 2 (two moves) is a third-order ... C, c = a + b = 3 ( three kinds of walking . method)
then we assign b a, c assigned b, (at this time is a second order, b is the third order) and then perform a c = a + b; (c natural order is the fourth)
by the such push, n-th order can be obtained in several moves.

Then, we put it into high-precision writing, ok. . . . . .

Code

#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 5005;

int a[maxn],b[maxn],c[maxn];
int n;

int main(){
	a[1] =1;
	b[1] =2;
	cin>>n;
	
	// c =a+b;   b=c; a=b;
	int lc=1;
	for(int i=3;i<=n;i++){
	//注意每次要先清0.
		memset(c,0,sizeof(c));
		for(int j=1;j<=lc;j++){
			c[j] += a[j] +b[j];
			c[j+1] += c[j]/10;
			c[j] %= 10;
		}
		//a= b; 
		if(c[lc+1]>0)lc++;
		for(int j=1;j<=lc;j++){
			a[j] = b[j];
		}
		//b =c;
		for(int j=1;j<=lc;j++){
			b[j] = c[j];
		}
	}
	
	if(n<3){
		cout<<n<<endl;
		return 0;
	} 
	for(int i=lc;i>=1;i--){
		cout<<c[i];
	}
	return 0;	
}

Published 75 original articles · won praise 1 · views 3633

Guess you like

Origin blog.csdn.net/A793488316/article/details/104832559