UVA10236 斐波那契素数
题意:任取斐波那契数列中一项f[i],若对于所有j
解法:这题的理论分析在黑书上有,结论是从第五项开始下标为素数的斐波那契数都是斐波那契素数
#include <stdio.h> #include <string.h> const int MAXN = 250010;; int prime[25010]; bool isprime[MAXN]; long double fib[MAXN]; int main(){ int i, j; for(i=0; i<MAXN; i++) isprime[i] = true; for(i=2; i<=500; i++){ for(j=i*i; j<=MAXN; j+=i) isprime[j] = false; } int idx = 1; for(i=2; i<=MAXN; i++){ if(isprime[i]) prime[idx++] = i; } prime[1] = 3; prime[2] = 4; fib[0] = fib[1] = 1; bool flag = false; for(i=2; i<=MAXN; i++){ if(flag){ fib[i] = fib[i-1] + fib[i-2]/10; flag = 0; } else { fib[i] = fib[i-1] + fib[i-2]; } if(fib[i]>1e9){ fib[i]/=10; flag = true; } } int n; while(scanf("%d", &n)!=EOF) printf("%d\n", (int)fib[prime[n]-1]); }
Greatness is never a given, it must be earned.