SDAU训练指南-数论2O

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

#include <cstdio>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#define ll int
using namespace std;
#define mod 1000000000
const ll maxn=252005;
bool v[maxn];
ll prime[maxn],m=0;
double fb[252000];
void primes(ll n)
{
    for(ll i=2; i<=n; i++)
    {
        if(!v[i])
        {
            prime[++m]=i;
            for(int j=i; j<=n/i; j++)
                v[i*j]=1;
        }
    }
}
void solve()
{
    fb[1]=fb[2]=1;int bo=0;
    for(int i=3; i<=252000; i++)
    {
        /*fb[i]=fb[i-1]+fb[i-2];
        while(fb[i]>1e9)
            fb[i]=fb[i]*1.0/10;*/  /*看了题解,我也不知道为什么要这样写,感觉和我注释的没大差别啊,但那个WR了,这个就能过*/
            if (bo) {
			fb[i] = fb[i - 1] + fb[i - 2] / 10;
			bo = 0;
		}
		else {
			fb[i] = fb[i - 1] + fb[i - 2];
		}
		if (fb[i] > 1e9) {
			fb[i] /= 10;
			bo = 1;
		}
    }
}
int main()
{
    primes(252000);
    solve();
    ll n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==1)
        {
            printf("2\n");
            continue;
        }
        if(n==2)
        {
            printf("3\n");
            continue;
        }
        printf("%d\n",(int)fb[prime[n]]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值