hdu 1250 Hat's Fibonacci

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1250

Hat's Fibonacci

Description

A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
$F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n > 4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)$
Your task is to take a number as input, and print that Fibonacci number.

Input

Each line will contain an integers. Process to end of file.

Output

For each case, output the result in a line.

SampleInput

100

SampleOutput

4203968145672990846840663646

大数加法模板题。。

  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<map>
 10 #include<set>
 11 using std::cin;
 12 using std::max;
 13 using std::cout;
 14 using std::endl;
 15 using std::string;
 16 using std::istream;
 17 using std::ostream;
 18 #define sz(c) (int)(c).size()
 19 #define all(c) (c).begin(), (c).end()
 20 #define iter(c) decltype((c).begin())
 21 #define cls(arr,val) memset(arr,val,sizeof(arr))
 22 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
 23 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
 24 #define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++)
 25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
 26 #define pb(e) push_back(e)
 27 #define mp(a, b) make_pair(a, b)
 28 struct BigN {
 29     typedef unsigned long long ull;
 30     static const int Max_N = 2010;
 31     int len, data[Max_N];
 32     BigN() { memset(data, 0, sizeof(data)), len = 0; }
 33     BigN(const int num) {
 34         memset(data, 0, sizeof(data));
 35         *this = num;
 36     }
 37     BigN(const char *num) {
 38         memset(data, 0, sizeof(data));
 39         *this = num;
 40     }
 41     void clear() { len = 0, memset(data, 0, sizeof(data)); }
 42     BigN& clean(){ while (len > 1 && !data[len - 1]) len--;  return *this; }
 43     string str() const {
 44         string res = "";
 45         for (int i = len - 1; ~i; i--) res += (char)(data[i] + '0');
 46         if (res == "") res = "0";
 47         res.reserve();
 48         return res;
 49     }
 50     BigN operator = (const int num) {
 51         int j = 0, i = num;
 52         do data[j++] = i % 10; while (i /= 10);
 53         len = j;
 54         return *this;
 55     }
 56     BigN operator = (const char *num) {
 57         len = strlen(num);
 58         for (int i = 0; i < len; i++) data[i] = num[len - i - 1] - '0';
 59         return *this;
 60     }
 61     BigN operator + (const BigN &x) const {
 62         BigN res;
 63         int n = max(len, x.len) + 1;
 64         for (int i = 0, g = 0; i < n; i++) {
 65             int c = data[i] + x.data[i] + g;
 66             res.data[res.len++] = c % 10;
 67             g = c / 10;
 68         }
 69         return res.clean();
 70     }
 71     BigN operator * (const BigN &x) const {
 72         BigN res;
 73         int n = x.len;
 74         res.len = n + len;
 75         for (int i = 0; i < len; i++) {
 76             for (int j = 0, g = 0; j < n; j++) {
 77                 res.data[i + j] += data[i] * x.data[j];
 78             }
 79         }
 80         for (int i = 0; i < res.len - 1; i++) {
 81             res.data[i + 1] += res.data[i] / 10;
 82             res.data[i] %= 10;
 83         }
 84         return res.clean();
 85     }
 86     BigN operator * (const int num) const {
 87         BigN res;
 88         res.len = len + 1;
 89         for (int i = 0, g = 0; i < len; i++) res.data[i] *= num;
 90         for (int i = 0; i < res.len - 1; i++) {
 91             res.data[i + 1] += res.data[i] / 10;
 92             res.data[i] %= 10;
 93         }
 94         return res.clean();
 95     }
 96     BigN operator - (const BigN &x) const {
 97         assert(x <= *this);
 98         BigN res;
 99         for (int i = 0, g = 0; i < len; i++) {
100             int c = data[i] - g;
101             if (i < x.len) c -= x.data[i];
102             if (c >= 0) g = 0;
103             else g = 1, c += 10;
104             res.data[res.len++] = c;
105         }
106         return res.clean();
107     }
108     BigN operator / (const BigN &x) const {
109         BigN res, f = 0;
110         for (int i = len - 1; ~i; i--) {
111             f *= 10;
112             f.data[0] = data[i];
113             while (f >= x) {
114                 f -= x;
115                 res.data[i]++;
116             }
117         }
118         res.len = len;
119         return res.clean();
120     }
121     BigN operator % (const BigN &x) {
122         BigN res = *this / x;
123         res = *this - res * x;
124         return res;
125     }
126     BigN operator += (const BigN &x) { return *this = *this + x; }
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     bool operator <  (const BigN &x) const {
132         if (len != x.len) return len < x.len;
133         for (int i = len - 1; ~i; i--) {
134             if (data[i] != x.data[i]) return data[i] < x.data[i];
135         }
136         return false;
137     }
138     bool operator >(const BigN &x) const { return x < *this; }
139     bool operator<=(const BigN &x) const { return !(x < *this); }
140     bool operator>=(const BigN &x) const { return !(*this < x); }
141     bool operator!=(const BigN &x) const { return x < *this || *this < x; }
142     bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); }
143     friend istream& operator >> (istream &in, BigN &x) {
144         string src;
145         in >> src;
146         x = src.c_str();
147         return in;
148     }
149     friend ostream& operator << (ostream &out, const BigN &x) {
150         out << x.str();
151         return out;
152     }
153 }A[5];
154 void solve(int n) {
155     fork(i, 1, 4) A[i] = 1;
156     if (n < 5) cout << A[n] << endl;
157     else {
158         int x = 1;
159         fork(i, 5, n) {
160             A[0] = A[1] + A[2] + A[3] + A[4];
161             A[x] = A[0];
162             if (++x == 5) x = 1;
163         }
164         cout << A[0] << endl;
165     }
166     rep(i, 5) A[i].clear();
167 }
168 int main() {
169 #ifdef LOCAL
170     freopen("in.txt", "r", stdin);
171     freopen("out.txt", "w+", stdout);
172 #endif
173     std::ios::sync_with_stdio(false);
174     int n;
175     while (cin >> n) {
176         solve(n);
177     }
178     return 0;
179 }
View Code

 

转载于:https://www.cnblogs.com/GadyPu/p/4574760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值