题目描述
给定一个整数n,求由“0”字符和“1”字符组成的长度为n的所有字符串中,满足“0”字符的左边必有“1”字符的字符串的数量。
输入描述:
输入一行,包含一个整数n。
输出描述:
输出一个整数,表示返回的答案,由于字符串的数量巨大,可能会溢出,请输出对2^29取模后的答案。
示例1
输入
1
输出
1
说明
只有“1”满足
示例2
输入
2
输出
2
说明
只有“10”和“11”满足
解法一:斐波那契
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int len = Integer.parseInt(br.readLine());
int res = getRes(len);
System.out.println(res);
}
public static int getRes(int len){
if(len<4) return len;
int res = 3;
int pre = 2;
for(int i=4;i<=len;i++){
int tmp = res;
res = res+pre;
res %= (int)Math.pow(2,29);
pre = tmp;
}
return res;
}
}