hdu Hat's Fibonacci(用了kuangbin模板)

大数的位数设置很坑,设成700会越界,设成800会超空间,最后设成了750居然就过了。。。。
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <cmath>
  6 #define LL long long
  7 using namespace std;
  8 
  9 /*
 10 * 完全大数模板
 11 * 输出cin>>a
 12 * 输出a.print();
 13 * 注意这个输入不能自动去掉前导0的,可以先读入到char数组,去掉前导0,再用构造函数。
 14 多组数据输入输出时在循环里面定义变量BigNum,在循坏外定义有时会出现WA
 15 大数的位数可以根据题目的要求进行更改
 16 */
 17 #define MAXN 9999
 18 #define MAXSIZE 1010
 19 #define DLEN 4
 20 class BigNum
 21 {
 22   private:
 23     int a[750]; //可以控制大数的位数
 24     int len;
 25 
 26   public:
 27     BigNum()
 28     {
 29         len = 1; //构造函数
 30         memset(a, 0, sizeof(a));
 31     }
 32     BigNum(const int);                               //将一个int类型的变量转化成大数
 33     BigNum(const char *);                            //将一个字符串类型的变量转化为大数
 34     BigNum(const BigNum &);                          //拷贝构造函数
 35     BigNum &operator=(const BigNum &);               //重载赋值运算符,大数之间进行赋值运算
 36     friend istream &operator>>(istream &, BigNum &); //重载输入运算符
 37     friend ostream &operator<<(ostream &, BigNum &); //重载输出运算符
 38     BigNum operator+(const BigNum &) const;          //重载加法运算符,两个大数之间的相加运算
 39     BigNum operator-(const BigNum &) const;          //重载减法运算符,两个大数之间的相减运算
 40     BigNum operator*(const BigNum &)const;           //重载乘法运算符,两个大数之间的相乘运算
 41     BigNum operator/(const int &) const;             //重载除法运算符,大数对一个整数进行相除运算
 42     BigNum operator^(const int &) const;             //大数的n次方运算
 43     int operator%(const int &) const;                //大数对一个int类型的变量进行取模运算
 44     bool operator>(const BigNum &T) const;           //大数和另一个大数的大小比较
 45     bool operator>(const int &t) const;              //大数和一个int类型的变量的大小比较
 46     void print();                                    //输出大数
 47 };
 48 BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
 49 {
 50     int c, d = b;
 51     len = 0;
 52     memset(a, 0, sizeof(a));
 53     while (d > MAXN)
 54     {
 55         c = d - (d / (MAXN + 1)) * (MAXN + 1);
 56         d = d / (MAXN + 1);
 57         a[len++] = c;
 58     }
 59     a[len++] = d;
 60 }
 61 BigNum::BigNum(const char *s) //将一个字符串类型的变量转化为大数
 62 {
 63     int t, k, index, L, i;
 64     memset(a, 0, sizeof(a));
 65     L = strlen(s);
 66     len = L / DLEN;
 67     if (L % DLEN)
 68         len++;
 69     index = 0;
 70     for (i = L - 1; i >= 0; i -= DLEN)
 71     {
 72         t = 0;
 73         k = i - DLEN + 1;
 74         if (k < 0)
 75             k = 0;
 76         for (int j = k; j <= i; j++)
 77             t = t * 10 + s[j] - '0';
 78         a[index++] = t;
 79     }
 80 }
 81 BigNum::BigNum(const BigNum &T) : len(T.len) //拷贝构造函数
 82 {
 83     int i;
 84     memset(a, 0, sizeof(a));
 85     for (i = 0; i < len; i++)
 86         a[i] = T.a[i];
 87 }
 88 BigNum &BigNum::operator=(const BigNum &n) //重载赋值运算符,大数之间赋值运算
 89 {
 90     int i;
 91     len = n.len;
 92     memset(a, 0, sizeof(a));
 93     for (i = 0; i < len; i++)
 94         a[i] = n.a[i];
 95     return *this;
 96 }
 97 istream &operator>>(istream &in, BigNum &b)
 98 {
 99     char ch[MAXSIZE * 4];
100     int i = -1;
101     in >> ch;
102     int L = strlen(ch);
103     int count = 0, sum = 0;
104     for (i = L - 1; i >= 0;)
105     {
106         sum = 0;
107         int t = 1;
108         for (int j = 0; j < 4 && i >= 0; j++, i--, t *= 10)
109         {
110             sum += (ch[i] - '0') * t;
111         }
112         b.a[count] = sum;
113         count++;
114     }
115     b.len = count++;
116     return in;
117 }
118 ostream &operator<<(ostream &out, BigNum &b) //重载输出运算符
119 {
120     int i;
121     cout << b.a[b.len - 1];
122     for (i = b.len - 2; i >= 0; i--)
123     {
124         printf("%04d", b.a[i]);
125     }
126     return out;
127 }
128 BigNum BigNum::operator+(const BigNum &T) const //两个大数之间的相加运算
129 {
130     BigNum t(*this);
131     int i, big;
132     big = T.len > len ? T.len : len;
133     for (i = 0; i < big; i++)
134     {
135         t.a[i] += T.a[i];
136         if (t.a[i] > MAXN)
137         {
138             t.a[i + 1]++;
139             t.a[i] -= MAXN + 1;
140         }
141     }
142     if (t.a[big] != 0)
143         t.len = big + 1;
144     else
145         t.len = big;
146     return t;
147 }
148 BigNum BigNum::operator-(const BigNum &T) const //两个大数之间的相减运算
149 {
150     int i, j, big;
151     bool flag;
152     BigNum t1, t2;
153     if (*this > T)
154     {
155         t1 = *this;
156         t2 = T;
157         flag = 0;
158     }
159     else
160     {
161         t1 = T;
162         t2 = *this;
163         flag = 1;
164     }
165     big = t1.len;
166     for (i = 0; i < big; i++)
167     {
168         if (t1.a[i] < t2.a[i])
169         {
170             j = i + 1;
171             while (t1.a[j] == 0)
172                 j++;
173             t1.a[j--]--;
174             while (j > i)
175                 t1.a[j--] += MAXN;
176             t1.a[i] += MAXN + 1 - t2.a[i];
177         }
178         else
179             t1.a[i] -= t2.a[i];
180     }
181     t1.len = big;
182     while (t1.a[t1.len - 1] == 0 && t1.len > 1)
183     {
184         t1.len--;
185         big--;
186     }
187     if (flag)
188         t1.a[big - 1] = 0 - t1.a[big - 1];
189     return t1;
190 }
191 BigNum BigNum::operator*(const BigNum &T) const //两个大数之间的相乘
192 {
193     BigNum ret;
194     int i, j, up;
195     int temp, temp1;
196     for (i = 0; i < len; i++)
197     {
198         up = 0;
199         for (j = 0; j < T.len; j++)
200         {
201             temp = a[i] * T.a[j] + ret.a[i + j] + up;
202             if (temp > MAXN)
203             {
204                 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
205                 up = temp / (MAXN + 1);
206                 ret.a[i + j] = temp1;
207             }
208             else
209             {
210                 up = 0;
211                 ret.a[i + j] = temp;
212             }
213         }
214         if (up != 0)
215             ret.a[i + j] = up;
216     }
217     ret.len = i + j;
218     while (ret.a[ret.len - 1] == 0 && ret.len > 1)
219         ret.len--;
220     return ret;
221 }
222 BigNum BigNum::operator/(const int &b) const //大数对一个整数进行相除运算
223 {
224     BigNum ret;
225     int i, down = 0;
226     for (i = len - 1; i >= 0; i--)
227     {
228         ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
229         down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
230     }
231     ret.len = len;
232     while (ret.a[ret.len - 1] == 0 && ret.len > 1)
233         ret.len--;
234     return ret;
235 }
236 int BigNum::operator%(const int &b) const //大数对一个 int类型的变量进行取模
237 {
238     int i, d = 0;
239     for (i = len - 1; i >= 0; i--)
240         d = ((d * (MAXN + 1)) % b + a[i]) % b;
241     return d;
242 }
243 BigNum BigNum::operator^(const int &n) const //大数的n次方运算
244 {
245     BigNum t, ret(1);
246     int i;
247     if (n < 0)
248         exit(-1);
249     if (n == 0)
250         return 1;
251     if (n == 1)
252         return *this;
253     int m = n;
254     while (m > 1)
255     {
256         t = *this;
257         for (i = 1; (i << 1) <= m; i <<= 1)
258             t = t * t;
259         m -= i;
260         ret = ret * t;
261         if (m == 1)
262             ret = ret * (*this);
263     }
264     return ret;
265 }
266 bool BigNum::operator>(const BigNum &T) const //大数和另一个大数的大小比较
267 {
268     int ln;
269     if (len > T.len)
270         return true;
271     else if (len == T.len)
272     {
273         ln = len - 1;
274         while (a[ln] == T.a[ln] && ln >= 0)
275             ln--;
276         if (ln >= 0 && a[ln] > T.a[ln])
277             return true;
278         else
279             return false;
280     }
281     else
282         return false;
283 }
284 bool BigNum::operator>(const int &t) const //大数和一个int类型的变量的大小比较
285 {
286     BigNum b(t);
287     return *this > b;
288 }
289 void BigNum::print() //输出大数
290 {
291     int i;
292     printf("%d", a[len - 1]);
293     for (i = len - 2; i >= 0; i--)
294         printf("%04d", a[i]);
295 }
296 
297 BigNum f[10000 + 100];
298 int main()
299 {
300     BigNum tmp(1);
301     f[1] = tmp;
302     f[2] = tmp;
303     f[3] = tmp;
304     f[4] = tmp;
305     for (int i = 5; i < 10000; i++)
306     {
307         f[i] = f[i - 1] + f[i - 2] + f[i - 3] + f[i - 4];
308     }
309     int x;
310     while (scanf("%d", &x) != EOF)
311     {
312         f[x].print();
313         printf("\n");
314     }
315 
316     return 0;
317 }

猜你喜欢

转载自www.cnblogs.com/whk19981229/p/10514055.html