zoukankan      html  css  js  c++  java
  • 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 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 leq b leq 10^{100}$ 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 $f_i$ with $a leq f_i leq b. $

    SampleInput

    10 100

    1234567890 9876543210

    0 0

    SampleOutput

    5

    4

      1 #include<algorithm>
      2 #include<iostream>
      3 #include<cstdlib>
      4 #include<cstring>
      5 #include<cassert>
      6 #include<cstdio>
      7 #include<vector>
      8 #include<string>
      9 #include<set>
     10 using std::cin;
     11 using std::max;
     12 using std::cout;
     13 using std::endl;
     14 using std::string;
     15 using std::vector;
     16 using std::istream;
     17 using std::ostream;
     18 #define N 510
     19 #define sz(c) (int)(c).size()
     20 #define all(c) (c).begin(), (c).end()
     21 #define iter(c) decltype((c).begin())
     22 #define cls(arr,val) memset(arr,val,sizeof(arr))
     23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
     24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
     25 #define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++)
     26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
     27 #define pb(e) push_back(e)
     28 #define mp(a, b) make_pair(a, b)
     29 struct BigN {
     30     typedef unsigned long long ull;
     31     static const int Max_N = 2010;
     32     int len, data[Max_N];
     33     BigN() { memset(data, 0, sizeof(data)), len = 0; }
     34     BigN(const int num) {
     35         memset(data, 0, sizeof(data));
     36         *this = num;
     37     }
     38     BigN(const char *num) {
     39         memset(data, 0, sizeof(data));
     40         *this = num;
     41     }
     42     void clear() { len = 0, memset(data, 0, sizeof(data)); }
     43     BigN& clean(){ while (len > 1 && !data[len - 1]) len--;  return *this; }
     44     string str() const {
     45         string res = "";
     46         for (int i = len - 1; ~i; i--) res += (char)(data[i] + '0');
     47         if (res == "") res = "0";
     48         res.reserve();
     49         return res;
     50     }
     51     BigN operator = (const int num) {
     52         int j = 0, i = num;
     53         do data[j++] = i % 10; while (i /= 10);
     54         len = j;
     55         return *this;
     56     }
     57     BigN operator = (const char *num) {
     58         len = strlen(num);
     59         for (int i = 0; i < len; i++) data[i] = num[len - i - 1] - '0';
     60         return *this;
     61     }
     62     BigN operator + (const BigN &x) const {
     63         BigN res;
     64         int n = max(len, x.len) + 1;
     65         for (int i = 0, g = 0; i < n; i++) {
     66             int c = data[i] + x.data[i] + g;
     67             res.data[res.len++] = c % 10;
     68             g = c / 10;
     69         }
     70         return res.clean();
     71     }
     72     BigN operator * (const BigN &x) const {
     73         BigN res;
     74         int n = x.len;
     75         res.len = n + len;
     76         for (int i = 0; i < len; i++) {
     77             for (int j = 0, g = 0; j < n; j++) {
     78                 res.data[i + j] += data[i] * x.data[j];
     79             }
     80         }
     81         for (int i = 0; i < res.len - 1; i++) {
     82             res.data[i + 1] += res.data[i] / 10;
     83             res.data[i] %= 10;
     84         }
     85         return res.clean();
     86     }
     87     BigN operator * (const int num) const {
     88         BigN res;
     89         res.len = len + 1;
     90         for (int i = 0, g = 0; i < len; i++) res.data[i] *= num;
     91         for (int i = 0; i < res.len - 1; i++) {
     92             res.data[i + 1] += res.data[i] / 10;
     93             res.data[i] %= 10;
     94         }
     95         return res.clean();
     96     }
     97     BigN operator - (const BigN &x) const {
     98         assert(x <= *this);
     99         BigN res;
    100         for (int i = 0, g = 0; i < len; i++) {
    101             int c = data[i] - g;
    102             if (i < x.len) c -= x.data[i];
    103             if (c >= 0) g = 0;
    104             else g = 1, c += 10;
    105             res.data[res.len++] = c;
    106         }
    107         return res.clean();
    108     }
    109     BigN operator / (const BigN &x) const {
    110         BigN res, f = 0;
    111         for (int i = len - 1; ~i; i--) {
    112             f *= 10;
    113             f.data[0] = data[i];
    114             while (f >= x) {
    115                 f -= x;
    116                 res.data[i]++;
    117             }
    118         }
    119         res.len = len;
    120         return res.clean();
    121     }
    122     BigN operator % (const BigN &x) {
    123         BigN res = *this / x;
    124         res = *this - res * x;
    125         return res;
    126     }
    127     BigN operator += (const BigN &x) { return *this = *this + x; }
    128     BigN operator *= (const BigN &x) { return *this = *this * x; }
    129     BigN operator -= (const BigN &x) { return *this = *this - x; }
    130     BigN operator /= (const BigN &x) { return *this = *this / x; }
    131     BigN operator %= (const BigN &x) { return *this = *this % x; }
    132     bool operator <  (const BigN &x) const {
    133         if (len != x.len) return len < x.len;
    134         for (int i = len - 1; ~i; i--) {
    135             if (data[i] != x.data[i]) return data[i] < x.data[i];
    136         }
    137         return false;
    138     }
    139     bool operator >(const BigN &x) const { return x < *this; }
    140     bool operator<=(const BigN &x) const { return !(x < *this); }
    141     bool operator>=(const BigN &x) const { return !(*this < x); }
    142     bool operator!=(const BigN &x) const { return x < *this || *this < x; }
    143     bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); }
    144     friend istream& operator >> (istream &in, BigN &x) {
    145         string src;
    146         in >> src;
    147         x = src.c_str();
    148         return in;
    149     }
    150     friend ostream& operator << (ostream &out, const BigN &x) {
    151         out << x.str();
    152         return out;
    153     }
    154 }A[N + 1], k1, k2;
    155 inline void init() {
    156     A[1] = 1, A[2] = 2;
    157     fork(i, 3, N) A[i] = A[i - 1] + A[i - 2];
    158 }
    159 int main() {
    160 #ifdef LOCAL
    161     freopen("in.txt", "r", stdin);
    162     freopen("out.txt", "w+", stdout);
    163 #endif
    164     init();
    165     char str1[N], str2[N];
    166     while (~scanf("%s %s", str1, str2)) {
    167         if (str1[0] == '0' && str2[0] == '0') break;
    168         int ans = 0;
    169         k1 = str1, k2 = str2;
    170         fork(i, 1, N) { if (k1 <= A[i] && A[i] <= k2) ans++; }
    171         printf("%d
    ", ans);
    172         k1.clear(), k2.clear();
    173     }
    174     return 0;
    175 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    Python异常处理
    Python 线程、进程和协程
    python版本升级及pip部署方法
    Python socket编程
    循环遍历方法笔记
    TCP/IP协议分为哪四层,具体作用是什么。
    Python Paramiko模块与MySQL数据库操作
    Python面向对象编程(二)
    h5专题常用小代码
    sethc问题
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4579347.html
Copyright © 2011-2022 走看看