poj 2413 How many Fibs? 打表+二分查找+大数加和模板

原创 2018年04月17日 19:12:34

点击打开链接

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxv=600+5;
string sum[maxv];
//大数加和模板
string bigsum(string s1,string s2)
{
     if(s1.length()<s2.length())
     {
          string temp=s1;
          s1=s2;
          s2=temp;
     }
     for(int i=s1.length()-1,j=s2.length ()-1;i>=0;i--,j--)
     {
          s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
          if(s1[i]-'0'>=10)
          {
              s1[i]=char((s1[i]-'0')%10+'0');
              if(i)
                   s1[i-1]++;
              else
                   s1='1'+s1;
          }
     }
     return s1;
}
bool judge(string s1,string s2)
{
    if(s2.size()==s1.size()) {
        if(s2>s1) return true;
    }
    else {
        if(s2.size()>s1.size()) return true;
    }
    return false;
}
//二分查找
int search(string s)
{
    int left=0,right=maxv;              // 左闭右开
    while(left<right)
    {
        int mid=(left+right)>>1;
        if(sum[mid]==s) return mid;
        else {
            if(judge(sum[mid],s)) left=mid+1;           //返回true 在靠右区间继续搜索
            else right=mid;
        }
    }
    return left;
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    sum[0]="1";
    sum[1]="2";
    for(int i=2;i<maxv;i++)
        sum[i]=bigsum(sum[i-1],sum[i-2]);
    string a,b;
    while(cin>>a>>b)
    {
        if(a=="0"&&b=="0") break;
        int p1=search(a);
        int p2=search(b);
        if(sum[p2]==b) p2++;
        cout<<p2-p1<<endl;
        a.clear();
        b.clear();
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37428263/article/details/79979236

WordPress博客模板系统开发

-
  • 1970年01月01日 08:00

POJ2413 How many Fibs?【高精度】【二分】

题目大意: 定义fibonacci数列前两项f[1] = 1,f[2] = 2。现在给你一个区间[a,b],a ...
  • u011676797
  • u011676797
  • 2015-04-18 09:40:29
  • 833

poj 2413 How many Fibs?

How many Fibs? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10748  ...
  • codeforcer
  • codeforcer
  • 2014-12-29 01:06:36
  • 892

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

现将前1000个的斐波那契数打表,然后再找就好了。 代码: import java.util.Scanner; import java.math.*; public class Mai...
  • shengweisong
  • shengweisong
  • 2014-09-07 21:31:43
  • 852

How Many Fibs?

题目来自杭电:http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs?Time Limit: 2000/1000 MS (Java/...
  • u012496793
  • u012496793
  • 2015-08-05 13:37:11
  • 370

HDU 1316 How Many Fibs?(大数+字符串数字比较)

先用大数打表,, ##include "string" #include "iostream" #include "cstdio" #include "cmath" #include "al...
  • secretbase_
  • secretbase_
  • 2015-03-29 22:15:53
  • 328

How Many Fibs? 【大数打表】

这道题一开始一点思路都没有,不过后来10^100感觉很大,dans
  • u013451221
  • u013451221
  • 2014-06-10 08:15:40
  • 583

How Many Fibs? (大数)

How Many Fibs? Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) T...
  • u012860428
  • u012860428
  • 2014-04-13 21:25:57
  • 683

HDU 1316-How Many Fibs?(大数类)

How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • qq_16255321
  • qq_16255321
  • 2014-09-15 22:21:29
  • 1040

UVa Problem 10183 How Many Fibs? (斐波那契计数)

// How Many Fibs? (斐波那契计数) // PC/UVa IDs: 110601/10183, Popularity: B, Success rate: average Level: ...
  • metaphysis
  • metaphysis
  • 2011-05-31 19:52:00
  • 2879
收藏助手
不良信息举报
您举报文章:poj 2413 How many Fibs? 打表+二分查找+大数加和模板
举报原因:
原因补充:

(最多只允许输入30个字)