POJ 2413 How many Fibs? /HDOJ 1316 How Many Fibs?

题目来源:http://poj.org/problem?id=2413 / http://acm.hdu.edu.cn/showproblem.php?pid=1316

How many Fibs?

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10742   Accepted: 3979

Description

Recall the definition of the Fibonacci numbers:

f1 := 1 

f2 := 2 

fn := fn-1 + fn-2     (n>=3) 

Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a,b].

Input

The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a=b=0. Otherwise, a<=b<=10100. The numbers a and b are given with no superfluous leading zeros.

Output

For each test case output on a single line the number of Fibonacci numbers fi with a<=fi<=b.

Sample Input

10 100
1234567890 9876543210
0 0

Sample Output

5
4

Source

Ulm Local 2000

题意: 求区间[a,b] 上Fibonacci 数的个数。

题解: 大数(用了压位高精度)+打表

PS: 我下面这个代码要在杭电上提交,编译器得选C++ 才给过 在POJ上G++是可以过的=-=  为此我还在知乎上提问了http://www.zhihu.com/question/27057931/answer/35115406?group_id=527458265756372992

AC代码:

#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
#define carry 100000000
#define maxn 600
using namespace std;
ifstream fin("in.txt");
ofstream fout("t.txt");
#define cin fin
#define cout fout
string num[maxn];
void str2num(string &x, int *num){
	int len = x.size(), pos = 0;
	for (int i = 1; i <= len / 8; i++){
		string temp = x.substr(len - 8 * i, 8);
		for (int i = 0; i<8; i++)
			num[pos] += (temp[7 - i] - '0')*(int)pow(10.0,double(i));
		pos++;
	}
	int left = len % 8;
	if (left){
		for (int i = 0; i < left; i++)
			num[pos] += (x[left - i - 1] - '0')*(int)pow(10.0,double(i));
		pos++;
	}
}
void num2str(int x, string &str){
	int temp[10] = { 0 }, pos = 0;
	while (x){
		temp[pos++] = x % 10;
		x /= 10;
	}
	for (int i = 7; i >= 0; i--)
		str += temp[i] + '0';
}
void Add(string x, string & y, string &z)
{
	string rest = "";
	int num1[maxn] = { 0 }, num2[maxn] = { 0 };
	int len = x.size() > y.size() ? x.size() : y.size();
	str2num(x, num1);
	str2num(y, num2);
	for (int i = 0; i <= len / 8; i++){
		num1[i] += num2[i];
		if (num1[i] >= carry){
			num1[i + 1] += num1[i] / carry;
			num1[i] %= carry;
		}
	}
	int k;
	for (k = len; k>0 && !num1[k]; k--);
	for (; k >= 0; k--) num2str(num1[k], rest);
	z = rest;
}
int cmp(string &x, string &y){
	int len1 = x.size(), len2 = y.size();
	int i;
	for (i = 0; i<len1&&!(x[i] - '0');)i++;
	x = x.substr(i, len1 - i + 1);
	//	cout<<i<<endl;
	for (i = 0; i<len2&&!(y[i] - '0');)i++;
	y = y.substr(i, len2 - i + 1);
	//	cout<<x<<" "<<y<<endl;
	len1 = x.size(), len2 = y.size();
	if (x == y)return 0;
	if (len1>len2)return 1;
	else if (len1<len2)return -1;
	else if (x>y)return 1;
	else return -1;
}
string str1, str2;
int calc(string &x, int left, int right, int dir)
{

	int cmpleft = cmp(x, num[left]), cmpright = cmp(x, num[right]), mid = (left + right) / 2;
	if (cmpleft<0){ if (dir)  return left;  else return left - 1; }
	else if (cmpright>0){ if (!dir) return right; else return right +1; }
	else if (!cmpleft)return left;
	else if (!cmpright)return right;
	int cmpmid = cmp(x, num[mid]);
	if (!cmpmid)return mid;
	else if (cmpmid<0)return calc(x, left, mid - 1,dir);
	else return calc(x, mid + 1, right, dir);
}

时间: 2024-10-12 12:31:27

POJ 2413 How many Fibs? /HDOJ 1316 How Many Fibs?的相关文章

HDOJ 1316 How Many Fibs?

JAVA大数.... How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3906    Accepted Submission(s): 1545 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 :=

hdoj 1316 How Many Fibs? 【Java大数】+【打表】

现将前1000个的斐波那契数打表,然后再找就好了. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger[] s = new BigInteger[1005]; s[1] = new BigInteger("1"); s[2]

poj 2413 How many Fibs?

How many Fibs? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10748   Accepted: 3982 Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n>=3) Given two numbers a and b, calculate how many Fibo

hdu 1316 How Many Fibs?

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs? Description Recall the definition of the Fibonacci numbers: $f_1 := 1$ $f_2 := 2$ $f_n := f_{n-1} + f_{n-2} \ \ (3 \leq n)$ Given two numbers a and b, calculate how many Fibonacci num

hdu 1316 How Many Fibs? (模拟高精度)

题目大意: 问[s,e]之间有多少个 斐波那契数. 思路分析: 直接模拟高精度字符串的加法和大小的比较. 注意wa点再 s 可以从 0 开始 那么要在判断输入结束的时候注意一下. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct node { char str[111]; int len; void

HDU 1316 How Many Fibs?(java,简单题,大数)

题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较. 对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=)中的每一个运算符的各个方法,优先提供此方法. 执行这些比较的建议语句是:(x.compareTo(y) <op> 0), 其中 <op> 是

hdu 1316 How many Fibs?(高精度斐波那契数)

//  大数继续 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n >= 3) Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b]. Input The input contains several test cas

HDUJ 1316 How Many Fibs?

How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4106    Accepted Submission(s): 1623 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-

POJ 2413 How many Fibs?#二分+大数加法

http://poj.org/problem?id=2413 #include<iostream> #include<cstdio> #include<cstring> using namespace std; //到第485个fib数才有100位 const int LAST=108; char res[500][110]; //存储fib数 char *pos[500]; //存储每个fib数的首地址 char* Addition(char *a,char *b,c