2834 Fibonacci Numbers

Source: Internet
Author: User

2834 Fibonacci Numbers

time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingView Run ResultsTitle 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 the data, the 0

Category labels Tags Click here to expand

Analysis :

Test instructions: Seek a Fibonacci number to satisfy the data range of F[i]%p==n (M is required f[i]) output I (he is in the position of the sequence)

Release code :

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intf[10110]={0,1,1},n,p,m;intMain () {scanf ("%d%d%d",&n,&p,&m); if(n==0|| n==1) {printf ("%d\n", F[n]);return 0;}  for(intI=2; i<=m;i++) {F[i]= (f[i-1]%p+f[i-2]%p)%p;//Congruence and modulus arithmetic        if(f[i]%p==n) {printf ("%d\n", i);return 0;} } printf ("-1\n"); return 0;}

2834 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.