Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
晕死,结果和别人的一样,就是WA哪位大侠指点下! 1-18的最高位分别为: 2 3 7 43 1807 3263443 10650 113423713 12864938 165506 27 750 563019 316 100483 10 101 10393 #include <stdio.h> #include <stdlib.h> #include <string.h> /****************************** *written by windy robin ,7/29/08 *a common high precision class windyrobin@hotmail.com ******************************/ typedef __int64 int64_t ; #define MIN(a ,b) ((a)<(b)?(a):(b)) #define MAX(a ,b) ((a)>(b)?(a):(b)) class HPALGO{ enum{ MAXINTNUM = 3000 ,//supoort max e(3000*9) SLICENUM = 1000000000//10^9 }; public: HPALGO(); HPALGO(int sInt); HPALGO(int sdata[] ,int maxLen=MAXINTNUM) ; // HPALGO(const HPALGO &s); void print(); HPALGO operator + (const HPALGO &s); HPALGO operator * (const HPALGO &s); HPALGO & operator += (const HPALGO &s) ; private : int data[MAXINTNUM+1] ; int maxPosIndex ; }; HPALGO::HPALGO() { memset(data ,0 ,sizeof(data)) ; maxPosIndex=0 ; } HPALGO::HPALGO(int sInt) { memset(data ,0 ,sizeof(data)) ; data[0] = sInt % SLICENUM ; data[1] = sInt / SLICENUM ; maxPosIndex = (sInt<SLICENUM ? 0 : 1) ; } HPALGO::HPALGO(int sdata[] ,int maxLen) { memset(data ,0 ,sizeof(data)) ; int i; int rLen=MIN(maxLen ,MAXINTNUM) ; for(i=0 ;i<rLen ;i++) data[i] = sdata[i] ; i=rLen - 1; while(i>=0 && data[i]==0 ){ i-- ; } maxPosIndex = MAX(i ,0); } HPALGO HPALGO::operator + (const HPALGO &s) { int temp; int result[MAXINTNUM+1] = {0} ; int rMaxPos=MAX(s.maxPosIndex ,maxPosIndex) ; for(int i=0 ;i<=rMaxPos ;i++){ temp = data[i] + s.data[i] ; temp += result[i] ; result[i] = temp % SLICENUM ; //max is 1 !!! result[i+1] = temp / SLICENUM ; } return HPALGO(result) ; } HPALGO HPALGO::operator * (const HPALGO &s) { int64_t temp ; int result[2*MAXINTNUM+1]={0} ; int maxResultPosIndex=maxPosIndex + s.maxPosIndex ; for(int i=0 ;i<=maxResultPosIndex ;i++){ for(int j=0 ;j<=i && j<=maxPosIndex ;j++){ if(i-j > s.maxPosIndex) continue ; //very important!! if not convert type ,will return int //this bug trouble me a lot!! temp = (int64_t)data[j] * s.data[i-j] ; temp += result[i] ; result[i] = (int)(temp % SLICENUM) ; result[i+1] += (int)(temp / SLICENUM) ; } } //if result exceed maxintnum ,only small part of it returned!! return HPALGO(result) ; } HPALGO& HPALGO::operator += (const HPALGO &s) { int i; int temp; int result[MAXINTNUM+1] = {0} ; int rMaxPos = MAX(maxPosIndex ,s.maxPosIndex) ; for(i=0 ;i<=rMaxPos ;i++){ //temp would not exceed 2100000000 //for data[i] s.data[i] is smaller than 10^9 //adn result[i] is 1 or 0 temp = data[i] + s.data[i] ; temp += result[i] ; result[i] = temp % SLICENUM ; result[i+1] = temp / SLICENUM ; } memcpy(data ,result ,sizeof(result)); i = rMaxPos + 1 ; while(i>=0 && data[i]==0 ){ i-- ; } maxPosIndex = MAX(i ,0); return *this ; } void HPALGO::print() { int i=maxPosIndex ; printf("%d" ,data[i--]) ; for( ;i>=0;i--){ printf("%09d" ,data[i]) ; } printf("\n") ; } void calHeritage(int n) { HPALGO remain=1 ; HPALGO cur ; for(int i=0 ;i<n ;i++){ cur = remain + 1; remain =remain * cur ; cur.print(); } } int main() { int n; scanf("%d" ,&n) ; calHeritage(n); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator