The Fibonacci number sequence is 1, 1, 2, 3, 5, 8, 13 and so on. You can see that except the first two numbers the others are summation of their previous two numbers. A Fibonacci Prime is a Fibonacci number which is relatively prime to all the smaller Fibonacci numbers. First such Fibonacci Prime is 2, the second one is 3, the third one is 5, the fourth one is 13 and so on. Given the serial of a Fibonacci Prime you will have to print the first nine digits of it. If the number has less than nine digits then print all the digits.
Input
The input file contains several lines of input. Each line contains an integer N(0<N<=22000) which indicates the serial of a Fibonacci Prime. Input is terminated by End of File.
Output
For each line of input produce one line of output which contains at most nine digits according to the problem statement.
Sample Input
Sample Output
(World Finals Warmup Contest , Problem setter: Shahriar Manzoor)
若某Fibonacci数与任何比它小的Fibonacci数互质,那么它就是Fibonacci质数。
1. F(3)和F(4)是Fibonacci质数;从F(5)开始,某项为Fibonacci质数当且仅当它的项数为质数
2. 第k小的Fibonacci质数是以质数数列中的第k个数为项数的Fibonacci数( 除F(3)和F(4)之外 )
关键是要保留前9位数!
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> using namespace std; const int maxn = 22010; const int maxm = 300010; const int INF = 1e9; double fibo[maxm]; vector<int> prime; int n; bool isprime[maxm]; void init(){ prime.clear(); prime.push_back(-1); memset(isprime,0,sizeof isprime); for(long long i = 2; i <= maxm; i++) if(!isprime[i]){ prime.push_back(i); for(long long j = i*i ; j <= maxm; j += i) isprime[j] = 1; } prime[1] = 3; prime[2] = 4; fibo[1] = 1; fibo[2] = 1; fibo[3] = 2; fibo[4] = 3; bool flag = 0; for(int i = 5; i < maxm; i++){ if(flag){ fibo[i] = fibo[i-1]+fibo[i-2]/10; flag = 0; }else{ fibo[i] = fibo[i-1]+fibo[i-2]; } if(fibo[i]>INF){ fibo[i]/=10; flag = 1; } } } int main(){ init(); while(cin >> n){ cout<<int(fibo[prime[n]])<<endl; } return 0; }
UVa10236 - The Fibonacci Primes,布布扣,bubuko.com
UVa10236 - The Fibonacci Primes
原文:http://blog.csdn.net/mowayao/article/details/21871829