2019 South University computer examination questions c
Title Description
Existing order n stairs, PIPI come from the first-order n-th order, each across a stage or two-stage, ask PIPI first order came from the n-th order total number of possibilities. In order to prevent the result is too large, need to make the results of the modulo p.
ps: p = 1e9 + 7.
Entry
Enter multiple sets of test cases.
The first line of each sample input order stairs n. (1 <= n <= 1000000 )
Export
For the number of samples, each output scheme. Finally, the results of 109 + 7 mod.
Sample input
1
2
3
Sample Output
1
2
3
Topic ideas
This is to check personal feeling problem, a recursive direct get away, f [n] = f [n-1] + f [n-2]. The place is a little pit modulo this place. We must first know a formula (a + b)% mod = (a% mod + b% mod)% mod.
The Code
#include<cstdio>
int f[1000005];
int mod = 1e9 + 7;
int main()
{
int n;
f[1] = 1;
f[2] = 2;
for(int i = 3; i <= 1000000; i++)
{
f[i] = f[i-1] + f[i-2];
f[i] %= mod;
}
while(scanf("%d", &n) != EOF)
{
printf("%d\n", f[n]);
}
return 0;
}