13. Fibonacci Numbers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.