Portal
Portal1: Nowcoder
Description
Beyond sister really likes her name, that she only likes the letters \ (\ textrm { "c" } \) and \ (\ {textrm "the y-"} \) . Thus contains only beyond sister like \ (\ textrm { "c" } \) and \ (\ textrm { "y" } \) of the string, and the string can not be two consecutive \ (\ textrm { " C "} \) . Please find how many of length \ (n \) strings are beyond the sister school like string. The answer to \ (1e9 + 7 \) modulo.
Input
Enter an integer \ (n-\) .
\ (1 \ n \ the 100000 \) .
Output
Output a integer answer.
Sample Input
3
Sample Output
5
Sample Explain
\(\textrm{cyy, cyc, yyy, yyc, ycy}\)。
Solution
We can see by enumerating
When \ (n = 1 \) , the answer is \ (2 \) : c, y
;
When \ (n = 2 \) , the answer is \ (3 \) : cy, yc, yy
;
When \ (n = 3 \) , the answer is \ (5 \) : cyy, cyc, yyy, yyc, ycy
;
When \ (n = 4 \) , the answer is \ (8 \) : yyyy, yyyc, yycy, ycyy, cyyy, cycy, yccy, ycyc
;
When \ (n = 5 \) , the answer is \ (13 \) : yyyyy, yyyyc, yyycy, yycyy, ycyyy, cyyyy, yycyc, ycyyc, cyyyc, ycycy, cyycy, cycyy, cycyc
;
\(\cdots \cdots\)
Easily summed law: \ (\ TEXTRM {F (I) = F (I -. 1) + F (I - 2)} (X \ GE. 3) \)
When finished the code required for the \ (n = 1 \) Patent judgment time.
Code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int n;
int main() {
scanf("%d", &n);
if (n == 1) {//特判n = 1的情况
printf("2\n");
return 0;
}
LL x1 = 2, x2 = 3;
for (int i = 3; i <= n; i++) {
LL tmp = x1;
x1 = x2 % mod;
x2 = (x2 + tmp) % mod;//前两项的和
}
printf("%lld\n", x2 % mod);//不要忘记取余
return 0;
}