UVa10236 - The Fibonacci Primes

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

(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;
    }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值