uva10236The Fibonacci Primes
题意:斐波那契素数的概念:任取斐波那契数列中的某一项Fi,对于所有小于Fi的斐波那契数,如果Fi都和它互为质数,那么Fi是斐波那契素数。输入n,输出第n个斐波那契素数。
分析:黑书上有这题的详细分析,大意是说从第5项开始下标为素数的斐波那契数一定是斐波那契素数。
代码:
View Code
1 #include <stdio.h> 2 #include <string.h> 3 const int MAXN = 250010;; 4 int prime[25010]; 5 bool isprime[MAXN]; 6 long double fib[MAXN]; 7 int main(){ 8 int i, j; 9 for(i=0; i<MAXN; i++) 10 isprime[i] = true; 11 for(i=2; i<=500; i++){ 12 for(j=i*i; j<=MAXN; j+=i) 13 isprime[j] = false; 14 } 15 int idx = 1; 16 for(i=2; i<=MAXN; i++){ 17 if(isprime[i]) 18 prime[idx++] = i; 19 } 20 prime[1] = 3; 21 prime[2] = 4; 22 fib[0] = fib[1] = 1; 23 bool flag = false; 24 for(i=2; i<=MAXN; i++){ 25 if(flag){ 26 fib[i] = fib[i-1] + fib[i-2]/10; 27 flag = 0; 28 } 29 else { 30 fib[i] = fib[i-1] + fib[i-2]; 31 } 32 if(fib[i]>1e9){ 33 fib[i]/=10; 34 flag = true; 35 } 36 } 37 int n; 38 while(scanf("%d", &n)!=EOF) 39 printf("%d\n", (int)fib[prime[n]-1]); 40 }
Greatness is never a given, it must be earned.