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:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 10213 C ++ "how many pieces of land? "*/
- # Include <algorithm>
- # Include <cstdio>
- # Include <cstring>
- # Include <deque>
- # Include <fstream>
- # Include <iostream>
- # Include <list>
- # Include <map>
- # Include <queue>
- # Include <set>
- # Include <stack>
- # Include <string>
- # Include <vector>
- Using namespace STD;
- Typedef unsigned long ulonglong;
- String add (string const & X, string const & Y)
- {
- Int sumlen = max (X. Size (), Y. Size () + 1;
- String sum (sumlen, '0'), xtmp (sumlen, '0'), ytmp (sumlen, '0 ');
- Int xstart = sumlen-X. Size (), ystart = sumlen-y. Size ();
- Copy (X. Begin (), X. End (), xtmp. Begin () + xstart );
- Copy (Y. Begin (), Y. End (), ytmp. Begin () + ystart );
- For (INT I = sumlen-1, S, carry = 0; I> = 0; -- I, carry = s/10)
- Sum [I] = (S = xtmp [I] + ytmp [I]-'0'-'0' + carry) % 10 + '0 ';
- For (; sum [0] = '0'; sum. Erase (0, 1 )){}
- Return sum;
- }
- String muladd (ulonglong X, ulonglong y, ulonglong Z)
- {
- Int const shift = 31;
- Ulonglong X1 = x> shift, X2 = x & (1ll <shift)-1 );
- Ulonglong Y1 = Y> shift, y2 = Y & (1ll <shift)-1 );
- Ulonglong z1 = z> shift, Z2 = Z & (1ll <shift)-1 );
- Ulonglong high = x1 * Y1, mid = x1 * y2 + x2 * Y1 + Z1, low = x2 * y2 + Z2;
- String TMP;
- For (; high! = 0; high/= 10)
- TMP. append (1, high % 10 + '0 ');
- String highs (TMP. rbegin (), TMP. rend ());
- For (TMP. Clear (); Mid! = 0; Mid/= 10)
- TMP. append (1, mid % 10 + '0 ');
- String mids (TMP. rbegin (), TMP. rend ());
- For (TMP. Clear (); low! = 0; low/= 10)
- TMP. append (1, low % 10 + '0 ');
- String lows (TMP. rbegin (), TMP. rend ());
- For (INT I = 0; I <shift * 2; ++ I)
- Highs = add (highs, highs );
- For (INT I = 0; I <shift; ++ I)
- Mids = add (MIDS, MIDS );
- String Product = add (highs, MIDS );
- Product = add (product, lows );
- Return product;
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- Freopen (string (argv [0]) + ". In"). c_str (), "r", stdin );
- Freopen (string (argv [0]) + ". Out"). c_str (), "W", stdout );
- # Endif
- /* F (n) = (n, 4) + (n, 2) + 1 */
- Int ncases;
- Cin> ncases;
- While (ncases --> 0 ){
- Ulonglong N;
- Cin> N;
- If (n <4 ){
- Ulonglong fn = 1;
- For (INT I = 1; I <n; ++ I)
- FN * = 2;
- Cout <FN <'/N ';
- } Else if (n <1 <16 ){
- Ulonglong fn = N * (n-1)/2*(n-2)/3 * (n-3)/4;
- FN + = n % 2 = 0? N/2 * (n-1): (n-1)/2 * N;
- FN + = 1;
- Cout <FN <'/N ';
- } Else {
- Ulonglong x = N * (n-1)/2, y = (n-2) * (n-3)/2;
- If (X % 2 = 0) x/= 2;
- Else y/= 2;
- If (X % 3 = 0) x/= 3;
- Else y/= 3;
- Ulonglong z = (N % 2 = 0? N/2 * (n-1): (n-1)/2 * n );
- Z + = 1;
- String fn = muladd (x, y, z );
- Cout <FN <'/N ';
- }
- }
- Return 0;
- }