UVA 10236 - The Fibonacci Primes(斐波那契素数)

来源:互联网 发布:win10下ubuntu安装教程 编辑:程序博客网 时间:2024/05/22 11:50

Problem A
The Fibonacci Primes

Input: standard input
Output: standard output
Time Limit: 8 seconds
Memory Limit: 32 MB

 

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
1
2
3

 

Sample Output
2
3
5

题意:求斐波那契数列的第n个素数。

思路:先素数打表,然后斐波那契数列的f[i],有个定理,i为素数,f(i)为素数。然后题目要保留前9位,可以用double型来处理。

代码:

#include <stdio.h>#include <string.h>const int N = 2500010;int n, prime[N / 10];bool isprime[N];double fib[N];void Is_Prime() {memset(isprime, 0, sizeof(isprime));for (int i = 2; i <= 500; i ++) {for (int j = i * i; j <= 250000; j += i) {isprime[j] = true;}}int num = 1;for (int k = 2; k <= 250000; k ++) {if (!isprime[k])prime[num ++] = k;}prime[1] = 3; prime[2] = 4;}void init() {Is_Prime();memset(fib, 0, sizeof(fib));fib[0] = fib[1] = 1; int bo =0;for (int i = 2; i <= 250000; i ++) {if (bo) {fib[i] = fib[i - 1] + fib[i - 2] / 10; bo = 0;}else {fib[i] = fib[i - 1] + fib[i - 2]; }if (fib[i] > 1e9) {fib[i] /= 10;bo = 1;}}}int main() {init();while (~scanf("%d", &n)) {printf("%d\n", (int)fib[prime[n] - 1]);}return 0;}