UV problem solution: 10213-how many pieces of land?

Source: Internet
Author: User

I use this formula to calculate: F (n) = (n, 4) + (n, 2) + 1. Detailed explanation is available here.

I really need to implement a big integer class. The following code is rather ugly.

Code:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 10213 C ++ "how many pieces of land? "*/
  6. # Include <algorithm>
  7. # Include <cstdio>
  8. # Include <cstring>
  9. # Include <deque>
  10. # Include <fstream>
  11. # Include <iostream>
  12. # Include <list>
  13. # Include <map>
  14. # Include <queue>
  15. # Include <set>
  16. # Include <stack>
  17. # Include <string>
  18. # Include <vector>
  19. Using namespace STD;
  20. Typedef unsigned long ulonglong;
  21. String add (string const & X, string const & Y)
  22. {
  23. Int sumlen = max (X. Size (), Y. Size () + 1;
  24. String sum (sumlen, '0'), xtmp (sumlen, '0'), ytmp (sumlen, '0 ');
  25. Int xstart = sumlen-X. Size (), ystart = sumlen-y. Size ();
  26. Copy (X. Begin (), X. End (), xtmp. Begin () + xstart );
  27. Copy (Y. Begin (), Y. End (), ytmp. Begin () + ystart );
  28. For (INT I = sumlen-1, S, carry = 0; I> = 0; -- I, carry = s/10)
  29. Sum [I] = (S = xtmp [I] + ytmp [I]-'0'-'0' + carry) % 10 + '0 ';
  30. For (; sum [0] = '0'; sum. Erase (0, 1 )){}
  31. Return sum;
  32. }
  33. String muladd (ulonglong X, ulonglong y, ulonglong Z)
  34. {
  35. Int const shift = 31;
  36. Ulonglong X1 = x> shift, X2 = x & (1ll <shift)-1 );
  37. Ulonglong Y1 = Y> shift, y2 = Y & (1ll <shift)-1 );
  38. Ulonglong z1 = z> shift, Z2 = Z & (1ll <shift)-1 );
  39. Ulonglong high = x1 * Y1, mid = x1 * y2 + x2 * Y1 + Z1, low = x2 * y2 + Z2;
  40. String TMP;
  41. For (; high! = 0; high/= 10)
  42. TMP. append (1, high % 10 + '0 ');
  43. String highs (TMP. rbegin (), TMP. rend ());
  44. For (TMP. Clear (); Mid! = 0; Mid/= 10)
  45. TMP. append (1, mid % 10 + '0 ');
  46. String mids (TMP. rbegin (), TMP. rend ());
  47. For (TMP. Clear (); low! = 0; low/= 10)
  48. TMP. append (1, low % 10 + '0 ');
  49. String lows (TMP. rbegin (), TMP. rend ());
  50. For (INT I = 0; I <shift * 2; ++ I)
  51. Highs = add (highs, highs );
  52. For (INT I = 0; I <shift; ++ I)
  53. Mids = add (MIDS, MIDS );
  54. String Product = add (highs, MIDS );
  55. Product = add (product, lows );
  56. Return product;
  57. }
  58. Int main (INT argc, char * argv [])
  59. {
  60. # Ifndef online_judge
  61. Freopen (string (argv [0]) + ". In"). c_str (), "r", stdin );
  62. Freopen (string (argv [0]) + ". Out"). c_str (), "W", stdout );
  63. # Endif
  64. /* F (n) = (n, 4) + (n, 2) + 1 */
  65. Int ncases;
  66. Cin> ncases;
  67. While (ncases --> 0 ){
  68. Ulonglong N;
  69. Cin> N;
  70. If (n <4 ){
  71. Ulonglong fn = 1;
  72. For (INT I = 1; I <n; ++ I)
  73. FN * = 2;
  74. Cout <FN <'/N ';
  75. } Else if (n <1 <16 ){
  76. Ulonglong fn = N * (n-1)/2*(n-2)/3 * (n-3)/4;
  77. FN + = n % 2 = 0? N/2 * (n-1): (n-1)/2 * N;
  78. FN + = 1;
  79. Cout <FN <'/N ';
  80. } Else {
  81. Ulonglong x = N * (n-1)/2, y = (n-2) * (n-3)/2;
  82. If (X % 2 = 0) x/= 2;
  83. Else y/= 2;
  84. If (X % 3 = 0) x/= 3;
  85. Else y/= 3;
  86. Ulonglong z = (N % 2 = 0? N/2 * (n-1): (n-1)/2 * n );
  87. Z + = 1;
  88. String fn = muladd (x, y, z );
  89. Cout <FN <'/N ';
  90. }
  91. }
  92. Return 0;
  93. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.