码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1250 Hat's Fibonacci

时间:2015-06-14 12:09:01      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

题目连接

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 typedef unsigned long long ull;
 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         while (!res.data[res.len - 1]) res.len--;
 71         return res;
 72     }
 73     BigN operator * (const BigN &x) const {
 74         BigN res;
 75         int n = x.len;
 76         res.len = n + len;
 77         for (int i = 0; i < len; i++) {
 78             for (int j = 0, g = 0; j < n; j++) {
 79                 res.data[i + j] += data[i] * x.data[j];
 80             }
 81         }
 82         for (int i = 0; i < res.len - 1; i++) {
 83             res.data[i + 1] += res.data[i] / 10;
 84             res.data[i] %= 10;
 85         }
 86         return res.clean();
 87     }
 88     BigN operator * (const int num) const {
 89         BigN res;
 90         res.len = len + 1;
 91         for (int i = 0, g = 0; i < len; i++) res.data[i] *= num;
 92         for (int i = 0; i < res.len - 1; i++) {
 93             res.data[i + 1] += res.data[i] / 10;
 94             res.data[i] %= 10;
 95         }
 96         return res.clean();
 97     }
 98     BigN operator - (const BigN &x) const {
 99         assert(x <= *this);
100         BigN res;
101         for (int i = 0, g = 0; i < len; i++) {
102             int c = data[i] - g;
103             if (i < x.len) c -= x.data[i];
104             if (c >= 0) g = 0;
105             else g = 1, c += 10;
106             res.data[res.len++] = c;
107         }
108         return res.clean();
109     }
110     BigN operator / (const BigN &x) const {
111         BigN res, f = 0;
112         for (int i = len - 1; ~i; i--) {
113             f *= 10;
114             f.data[0] = data[i];
115             while (f >= x) {
116                 f -= x;
117                 res.data[i]++;
118             }
119         }
120         res.len = len;
121         res.clean();
122         return res;
123     }
124     BigN operator % (const BigN &x) {
125         BigN res = *this / x;
126         res = *this - res * x;
127         return res;
128     }
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     BigN operator /= (const BigN &x) { return *this = *this / x; }
133     BigN operator %= (const BigN &x) { return *this = *this % x; }
134     bool operator <  (const BigN &x) const {
135         if (len != x.len) return len < x.len;
136         for (int i = len - 1; ~i; i--) {
137             if (data[i] != x.data[i]) return data[i] < x.data[i];
138         }
139         return false;
140     }
141     bool operator >(const BigN &x) const { return x < *this; }
142     bool operator<=(const BigN &x) const{ return !(x < *this); }
143     bool operator>=(const BigN &x) const{ return !(*this < x); }
144     bool operator!=(const BigN &x) const{ return x < *this || *this < x; }
145     bool operator==(const BigN &x) const{ return !(x < *this) && !(x > *this); }
146     friend istream& operator >> (istream &in, BigN &x) {
147         string src;
148         in >> src;
149         x = src.c_str();
150         return in;
151     }
152     friend ostream& operator << (ostream &out, const BigN &x) {
153         out << x.str();
154         return out;
155     }
156 }A[5];
157 void solve(int n) {
158     fork(i, 1, 4) A[i] = 1;
159     if (n < 5) cout << A[n] << endl;
160     else {
161         int x = 1;
162         fork(i, 5, n) {
163             A[0] = A[1] + A[2] + A[3] + A[4];
164             A[x] = A[0];
165             if (++x == 5) x = 1;
166         }
167         cout << A[0] << endl;
168     }
169     rep(i, 5) A[i].clear();
170 }
171 int main() {
172 #ifdef LOCAL
173     freopen("in.txt", "r", stdin);
174     freopen("out.txt", "w+", stdout);
175 #endif
176     std::ios::sync_with_stdio(false);
177     int n;
178     while (cin >> n) {
179         solve(n);
180     }
181     return 0;
182 }
View Code

 

hdu 1250 Hat's Fibonacci

标签:

原文地址:http://www.cnblogs.com/GadyPu/p/4574760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!