UVA 10236 - The Fibonacci Primes(斐波那契素数)
2013-11-22 16:37
1036 查看
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
;
double fib
;
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
- 1]);
}
return 0;
}
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
;
double fib
;
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
- 1]);
}
return 0;
}
相关文章推荐
- UVa 10236 - The Fibonacci Primes(斐波那契素数)
- UVa 10236 The Fibonacci Primes (斐波那契素数)
- UVA 10236 The Fibonacci Primes
- UVA 10236 The Fibonacci Primes
- uva 10236 The Fibonacci Primes
- uva 10236 - The Fibonacci Primes
- UVA 10236 The Fibonacci Primes
- UVA10236 斐波那契素数
- Uva 11582 Colossal Fibonacci Numbers! 快速幂,斐波那契
- 【2016-沈阳赛区网络赛-J】大素数模板(Count primes,uva 5901)
- UVA1213Sum of Different Primes(素数打表 + DP)
- UVA11752-The Super Powers(素数表+log)
- Sum of Different Primes UVA - 1213 不同素数之和的个数 dp
- Colossal Fibonacci Numbers! 巨大的斐波那契数 UVA - 11582
- UVa10236 - The Fibonacci Primes
- UVa 11752 (素数筛选 快速幂) The Super Powers
- Colossal Fibonacci Numbers! 巨大的斐波那契数 UVA - 11582
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
- UVA - 11752 The Super Powers 【预处理+素数筛选】
- Colossal Fibonacci Numbers! 巨大的斐波那契数 UVA - 11582