HDU 1250 Hat's Fibonacci(递推+大数加法)
Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence,with the first two members being both 1.
F(1) = 1,F(2) = 1,F(3) = 1,F(4) = 1,F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take a number as input,and print that Fibonacci number.
?
Input
Each line will contain an integers. Process to end of file.
?
Output
For each case,output the result in a line.
?
Sample Input
?
Sample Output
?
/************************************************************************/
题意:首先,该题给出了一个类似于斐波那契数列的数列的前4项以及它的递推式,要求我们求出第n项(关于n的范围,题目只告诉我们任何一项的值位数都在2005位以内) 解题思路:因为递推式也已经知道了,此题无非就是一道大数加法的题目,解决方案大体分为两种 ①对于给出的n,每次都从第5项开始重新计算,这样做节省了空间,但是时间复杂度就比较高了,几百MS应该是要的吧 ②另一种则无非是空间换时间,在预处理阶段就先提前算好所有项的值 第②方法15MS就差不多了,但是我们可以来算算,每一项至少要留出2005的空间,而根据测试 到 7060项 的时候长度为 2012位,那就至少得开一个7061*2012的字符数组 orz 难逃MLE的命运 然而,这里有一种甚妙的解决方法: 我们都清楚一点,平时模拟大数加法的时候,我们都是数组的1个单元表示数的一位,比如说1314,我们或许就开了s[4]的数组,每一下标地址存储1314的一位,其实,我们完全可以只开s[1]的数组,存取1314的四位,这样就节省了3/4的空间,当然,你也可以存5位或者6位等等 以下给出该数列一些项的值以供参考: 第1项:1 第2项: 1 第3项: 1 第4项: 1 第5项: 4 第6项: 7 第7项: 13 第8项: 25 第9项: 49 …… 第20项: 66526 …… 第100项: 4203968145672990846840663646 …… 第7060项: 19132815656249268518490884190898848150001617262347102273710143150756354315829469888479509852889801050570395030799042735599111306649112251672457076466650962498630829437743787496112628531755305195115824430566997259641082744720736373287527919747575700260158473417665109405226920319652777277465175585464477696741922610415720562637391000667082785866914124869954720605846206351934256829531337834907474790842402939533274168300465378314603690508388596785309644903523761912630349267978975515647065125864382428117352761163937139356224263020486240923594734374261980140548731752455902881133635264737507924434810945115433313620016890039236095984940057274493964779264477753883735193438337115328806060914311429064446374554426902401691918403227167000416965835533574488228673331213305584089827205822198178813616407932298992968043206307140383955468912432121406775274033553963870186469871676404048161402129097748941437460852413334088109674329206846676321465777732569214238064950886012062268873206493753190634789004799611556800637203227512723105856795530816513091583725538824859376293542708875845281047618923884839453333724411835902641527124883119228499146282120886161631896324333116263087312981248089803618917481853788515567591309175795141248776102795891234197235862536601235399047369123830729712393846351332113738084379456593883504192488947323489128239861181670430489724151486093702031373256044063969823485993332685133721886784746661986430213891375782817130484592345089812001975466681122284637215311187205376701799534964335815653819205997139763191833257419798477962899189856200584555253213674148433904217604249741835039321036538135988714461282791837758943144139298372850687703481995710114234187808790725517079076758858226460912581124809100937111136550336603385642706799893940658422716366496495750738210734489036386052479471041621146201924331588144572267285937979746436701951413929888154167243166012017529783432709278881378267793177030592421107611791666043469274080669610510569035691019602251413731655081987038208970322896071458782 #pragma comment(linker,"/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 7061; const int inf = 2147483647; const int mod = 2009; int s[N][504],l[N]; int main() { int n,i,j; s[1][0]=s[2][0]=s[3][0]=s[4][0]=1; l[1]=l[2]=l[3]=l[4]=1; for(i=5;i<N;i++) { for(j=0;j<l[i-1];j++) { s[i][j]+=s[i-1][j]+s[i-2][j]+s[i-3][j]+s[i-4][j]; s[i][j+1]+=s[i][j]/10000; s[i][j]%=10000; } while(s[i][j]>0) { s[i][j+1]+=s[i][j]/10000; s[i][j]%=10000;j++; } l[i]=j; } while(~scanf("%d",&n)) { printf("%d",s[n][l[n]-1]); for(i=l[n]-2;i>=0;i--) printf("%4.4d",s[n][i]); puts(""); } return 0; }菜鸟成长记 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |