ACM 2083 Game of Connections http://acm.pku.cn/JudgeOnline/problem?id=2084
很郁闷的一道题,看通过率本以为很简单,其实就是个数学题,求Catalan数,关于Catalan数,现在做下说明: 令h(1)=1,catalan数满足递归式: 这样,有了地推公式,这个题就转化成了一个求大整数的的乘法和加法的问题了!但是写出来后各种运行错误,无奈之际只好打表通过,先稳定下军心: code: #include <iostream> using namespace std; char kk [100][100]={ "1", "2", "5", "14", "42", "132", "429", "1430", "4862", "16796", "58786", "208012", "742900", "2674440", "9694845", "35357670", "129644790", "477638700", "1767263190", "6564120420", "24466267020", "91482563640", "343059613650", "1289904147324", "4861946401452", "18367353072152", "69533550916004", "263747951750360", "1002242216651368", "3814986502092304", "14544636039226909", "55534064877048198", "212336130412243110", "812944042149730764", "3116285494907301262", "11959798385860453492", "45950804324621742364", "176733862787006701400", "680425371729975800390", "2622127042276492108820", "10113918591637898134020", "39044429911904443959240", "150853479205085351660700", "583300119592996693088040", "2257117854077248073253720", "8740328711533173390046320", "33868773757191046886429490", "131327898242169365477991900", "509552245179617138054608572", "1978261657756160653623774456", "7684785670514316385230816156", "29869166945772625950142417512", "116157871455782434250553845880", "451959718027953471447609509424", "1759414616608818870992479875972", "6852456927844873497549658464312", "26700952856774851904245220912664", "104088460289122304033498318812080", "405944995127576985730643443367112", "1583850964596120042686772779038896", "6182127958584855650487080847216336", "24139737743045626825711458546273312", "94295850558771979787935384946380125", "368479169875816659479009042713546950", "1440418573150919668872489894243865350", "5632681584560312734993915705849145100", "22033725021956517463358552614056949950", "86218923998960285726185640663701108500", "337485502510215975556783793455058624700", "1321422108420282270489942177190229544600", "5175569924646105559418940193995065716350", "20276890389709399862928998568254641025700", "79463489365077377841208237632349268884500", "311496878311103321137536291518809134027240", "1221395654430378811828760722007962130791020", "4790408930363303911328386208394864461024520", "18793142726809884575211361279087545193250040", "73745243611532458459690151854647329239335600", "289450081175264899454283846029490767264392230", "1136359577947336271931632877004667456667613940", "4462290049988320482463241297506133183499654740", "17526585015616776834735140517915655636396234280", "68854441132780194707888052034668647142985206100", "270557451039395118028642463289168566420671280440", "1063353702922273835973036658043476458723103404520", "4180080073556524734514695828170907458428751314320", "16435314834665426797069144960762886143367590394940", "64633260585762914370496637486146181462681535261000", "254224158304000796523953440778841647086547372026600", "1000134600800354781929399250536541864362461089950800", "3935312233584004685417853572763349509774031680023800", "15487357822491889407128326963778343232013931127835600", "60960876535340415751462563580829648891969728907438000", "239993345518077005168915776623476723006280827488229600", "944973797977428207852605870454939596837230758234904050", "3721443204405954385563870541379246659709506697378694300", "14657929356129575437016877846657032761712954950899755100", "57743358069601357782187700608042856334020731624756611000", "227508830794229349661819540395688853956041682601541047340", "896519947090131496687170070074100632420837521538745909320" }; int main() { int m; while (cin >>m) { if (m==-1) break; cout<<kk[m-1]<<endl; } return 0; } 首先鄙视下自己的无耻行为,当时实在没办法了,理解下我吧!做不出来就睡不着觉的,后来才发现自己原来写的高精度乘法越界了。 后来干脆搞了个大整数的类,重载了各种需要的运算符了以后才把它名正言顺的AC过去! code: #include "string" #include "iostream" #include "iomanip" const long MaxLength=100;//数字最大位数是MaxLength *4 using namespace std; class BigInt { public: long *a; long length; long max; BigInt()//构造函数 { a = new long [MaxLength]; memset(a,0,sizeof(long)*MaxLength); length = 1; max = MaxLength; } BigInt(const BigInt&t) { long *a = new long [t.length]; for(int i=0;i<t.length;i++) a[i] = t.a[i]; length = t.length; max = t.max; } ~BigInt()//析构函数 { delete [] a; } void clear()//清零函数 { memset(a,0,sizeof(long)*max); length = 1; } friend ostream &operator << (ostream & output,BigInt &t) { output << t.a[t.length-1]; for(int i=t.length-2;i>=0;i--) output << setw(4) << setiosflags(ios::right) << setfill('0') << t.a[i]; return output; } void operator = (const BigInt & t) { if(t.a==a) return; delete a; a = new long [t.length]; for(int i=0;i<t.length;i++) a[i] = t.a[i]; length = t.length; } void operator = (const string & temp) { int tLength = temp.length() - 1; length=tLength/4+1; for(int i=0;tLength>2;i++,tLength-=4) a[i]=(temp[tLength]-'0')+(temp[tLength-1]-'0')*10+(temp[tLength-2]-'0')*100+(temp[tLength-3]-'0')*1000; if(tLength==2) a[length-1]=(temp[tLength]-'0')+(temp[tLength-1]-'0')*10+(temp[tLength-2]-'0')*100; else if(tLength==1) a[length-1]=(temp[tLength]-'0')+(temp[tLength-1]-'0')*10; else if(!tLength) a[length-1]=(temp[tLength]-'0'); } BigInt operator + (const BigInt & t) { BigInt sum; for(int i=0;i<t.length||i<length;i++) { sum.a[i]+=a[i]+t.a[i]; if(sum.a[i]>9999) { int j=i; while(sum.a[j]>9999) { sum.a[j+1]+=sum.a[j]/10000; sum.a[j]%=10000; j++; } } } for(int i=sum.max-1;i>=sum.length;i--) if(sum.a[i]) { sum.length=i+1; break; } return sum; } BigInt operator * (const BigInt & t) { BigInt product; for(int i=0;i<t.length;i++) for(int j=0;j<length;j++) { product.a[i+j]+=a[j]*t.a[i]; if(product.a[i+j]>9999) { int q=i+j; while(product.a[q]>9999) { product.a[q+1]+=product.a[q]/10000; product.a[q]%=10000; q++; } } } for(int i=product.max-1;i>=product.length;i--) if(product.a[i]) { product.length=i+1; break; } return product; } }; BigInt s[MaxLength+1]; int main() { int n; s[0]= "1"; s[1]="1";s[2]="2";s[3]="5"; for(int i=4;i<101;i++) for(int j=0;j<i;j++) // s[i] = add(s[i],mult(s[j],s[i-j])); s[i] = s[i]+s[j]*s[i-j-1]; while(cin>>n && n != -1) { cout<<s[n]<<endl; } return 0; } |