Advertisement
Saleh127

UVA 10497 (template)

Feb 17th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. typedef long long LL;
  7. const int K = 10000;//Each bit in the array represents 1W
  8. const int M = 300;//10 digits in total
  9. const char show [] = "% 04lld";
  10.  
  11. struct Bignum
  12. {
  13. LL a [M * ​​2];//array of large numbers
  14. int len;//length
  15. bool negative;//positive negative
  16.  
  17. Bignum ()
  18. {
  19. clear ();
  20. }
  21.  
  22. void clear ()
  23. {
  24. len = 0;
  25. negative = false;
  26. memset (a, 0, sizeof (a));
  27. }
  28.  
  29. Bignum (LL num)
  30. {
  31. * this = num;
  32. }
  33.  
  34. Bignum operator = (LL num)
  35. {
  36. clear ();
  37. if (num <0) negative = true, num = -num;
  38. while (num)
  39. a [len ++] = num% K, num/= K;
  40. return * this;
  41. }
  42.  
  43. Bignum (const Bignum & cmp)
  44. {
  45. memcpy (this, & cmp, sizeof (Bignum));
  46. }
  47.  
  48. Bignum operator = (const Bignum & cmp)
  49. {
  50. memcpy (this, & cmp, sizeof (Bignum));
  51. return * this;
  52. }
  53.  
  54. int absCmp (const Bignum & cmp)
  55. {
  56. if (len! = cmp.len)
  57. return len> cmp.len? 1: -1;
  58.  
  59. for (int i = len-1; i> = 0; i--)
  60. if (a [i]! = cmp.a [i])
  61. return a [i]> cmp.a [i]? 1: -1;
  62.  
  63. return 0;
  64. }
  65.  
  66. int absCmp (LL num)
  67. {
  68. Bignum cmp (num);
  69. return absCmp (cmp);
  70. }
  71.  
  72. bool operator <(const Bignum & cmp)
  73. {
  74. if (negative ^ cmp.negative)
  75. return negative? true: false;
  76.  
  77. if (negative)
  78. return absCmp (cmp)> 0;
  79. else
  80. return absCmp (cmp) <0;
  81. }
  82.  
  83. bool operator <(LL num)
  84. {
  85. Bignum cmp (num);
  86. return * this <cmp;
  87. }
  88.  
  89. bool operator == (const Bignum & cmp)
  90. {
  91. if (negative ^ cmp.negative)
  92. return false;
  93. return absCmp (cmp) == 0;
  94. }
  95.  
  96. bool operator == (LL num)
  97. {
  98. Bignum cmp (num);
  99. return * this == cmp;
  100. }
  101.  
  102. void absAdd (const Bignum & one, const Bignum & two)
  103. {
  104. len = max (one.len, two.len);
  105. for (int i = 0; i <len; i ++)
  106. {
  107. a [i] + = one.a [i] + two.a [i];
  108. if (a [i]> = K) a [i]-= K, a [i + 1] ++;
  109. }
  110. if (a [len]) len ++;
  111. }
  112.  
  113. void absSub (const Bignum & one, const Bignum & two)
  114. {
  115. len = one.len;
  116. for (int i = 0; i <len; i ++)
  117. {
  118. a [i] + = one.a [i] -two.a [i];
  119. if (a [i] <0) a [i + 1]-, a [i] + = K;
  120. }
  121. while (len> 0 && a [len-1] == 0) len--;
  122. }
  123.  
  124. void absMul (const Bignum & one, const Bignum & two)
  125. {
  126. len = one.len + two.len;
  127. for (int i = 0; i <one.len; i ++) for (int j = 0; j <two.len; j ++)
  128. a [i + j] + = one.a [i] * two.a [j];
  129. for (int i = 0; i <len; i ++) if (a [i]> = K)
  130. a [i + 1] + = a [i]/K, a [i]% = K;
  131. while (len> 0 && a [len-1] == 0) len--;
  132. }
  133.  
  134. Bignum operator + (const Bignum & cmp)
  135. {
  136. Bignum c;
  137. if (negative ^ cmp.negative)
  138. {
  139. bool res = absCmp (cmp)> 0;
  140. c.negative =! (negative ^ res);
  141. if (res)
  142. c.absSub (* this, cmp);
  143. else
  144. c.absSub (cmp, * this);
  145. }
  146. else if (negative)
  147. {
  148. c.negative = true;
  149. c.absAdd (* this, cmp);
  150. }
  151. else
  152. {
  153. c.absAdd (* this, cmp);
  154. }
  155. return c;
  156. }
  157.  
  158. Bignum operator- (const Bignum & cmp)
  159. {
  160. Bignum cpy;
  161. if (cpy == cmp)
  162. return * this;
  163. else
  164. cpy = cmp, cpy.negative ^ = true;
  165.  
  166. return * this + cpy;
  167. }
  168.  
  169. Bignum operator * (const Bignum & cmp)
  170. {
  171. Bignum c;
  172. if (c == cmp || c == * this)
  173. return c;
  174.  
  175. c.negative = negative ^ cmp.negative;
  176. c.absMul (* this, cmp);
  177. return c;
  178. }
  179.  
  180. void output ()
  181. {
  182. if (len == 0)
  183. {
  184. puts ("0");
  185. return;
  186. }
  187.  
  188. if (negative)
  189. printf ("-");
  190.  
  191. printf ("% lld", a [len-1]);
  192. for (int i = len-2; i> = 0; i--)
  193. printf (show, a [i]);
  194. puts ("");
  195. }
  196. };
  197.  
  198. Bignum f[801];
  199.  
  200. void fib()
  201. {
  202. f[1] = 0;
  203. f [2] = 1;
  204. for (int i = 3; i <= 800; i ++)
  205. f [i] = (f [i-1] + f [i-2]) * (i-1);
  206. }
  207. int main()
  208. {
  209. ios_base::sync_with_stdio(0);
  210. cin.tie(0);
  211. cout.tie(0);
  212.  
  213. fib();
  214.  
  215. int b;
  216. while(cin>>b && b!=-1)
  217. {
  218. cout<<f[b]<<endl;
  219. }
  220.  
  221.  
  222. return 0;
  223. }
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement