Fibonacci number
Time limit: 1 s
Space limit: 128000 KB
Title Level: Golden Gold
Exercises
View Run Results
Title Description Description
Little X is a clever boy who remembers the first 1000 numbers in the Fibonacci series F (N). But because of academic pressure, he could not remember the position of each number in the series.
He now knows a number in the Fibonacci sequence F (x) modulo p after the value N (i.e. f (x) mod p=n) and x possible maximum m, if the retracement in the sequence of every number of modulo p, he would like to know that this number may appear in the first few. But the small x also has to do the homework, this question is handed over to you by the programming to solve.
Enter a description Input Description
A row, a total of 3 integers, the first number is N, the second number is P, the third number is the maximum possible value of x m, and three numbers are separated by a space.
Output Description Output Description
An integer that satisfies the minimum I of the F (i) mod P = n and outputs-1 if it does not exist.
Sample Input Sample Input
3 7 5
Sample Output Sample Output
4
data range and tips Data Size & Hint
For 20% of data, ensure 0
For 50% of data, ensure 0
For 70% of data, ensure 0
For 100% of data, ensure 0
, p is prime and 2
5.
Code:
#include
using namespace Std;
#include
int n,p,m;
int f[1001];
int main ()
{
scanf ("%d%d%d", &n,&p,&m);
F[1]=1;f[2]=1;
if (n==1)
{
cout<<1<<endl;
return 0;
}
int flag=0;
for (int i=3;i<=m;++i)
{
F[i]= (f[i-1]+f[i-2])%p;
if (f[i]==n)
{
printf ("%d\n", I);
return 0;
}
}
if (flag==0)
{
printf (" -1\n");
return 0;
}
return 0;
}
13. Fibonacci Numbers