设为首页 加入收藏

TOP

HDU 4099 Revenge of Fibonacci(一)
2015-07-20 17:41:20 来源: 作者: 【 】 浏览:6
Tags:HDU 4099 Revenge Fibonacci

Revenge of Fibonacci

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 2027 Accepted Submission(s): 475


Problem Description The well-known Fibonacci sequence is defined as following:


Here we regard n as the index of the Fibonacci number F(n).
This sequence has been studied since the publication of Fibonacci's book Liber Abaci. So far, many properties of this sequence have been introduced.
You had been interested in this sequence, while after reading lots of papers about it. You think there’s no need to research in it anymore because of the lack of its unrevealed properties. Yesterday, you decided to study some other sequences like Lucas sequence instead.
Fibonacci came into your dream last night. “Stupid human beings. Lots of important properties of Fibonacci sequence have not been studied by anyone, for example, from the Fibonacci number 347746739…”
You woke up and couldn’t remember the whole number except the first few digits Fibonacci told you. You decided to write a program to find this number out in order to continue your research on Fibonacci sequence.
Input There are multiple test cases. The first line of input contains a single integer T denoting the number of test cases (T<=50000).
For each test case, there is a single line containing one non-empty string made up of at most 40 digits. And there won’t be any unnecessary leading zeroes.
Output For each test case, output the smallest index of the smallest Fibonacci number whose decimal notation begins with the given digits. If no Fibonacci number with index smaller than 100000 satisfy that condition, output -1 instead ? you think what Fibonacci wants to told you beyonds your ability.
Sample Input
15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610

Sample Output
Case #1: 0
Case #2: 25
Case #3: 226
Case #4: 1628
Case #5: 49516
Case #6: 15
Case #7: 15
Case #8: 15
Case #9: 43764
Case #10: 49750
Case #11: 10
Case #12: 51
Case #13: -1
Case #14: 1233
Case #15: 22374

Source 2011 Asia Shanghai Regional Contest 解题思路:大数+字典树,处理出前10w的fibonocci数的前40位加入到字典树中,大数处理的时候用50位,避免进位错误,注意会爆内存
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
         #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #define Maxn 100005 #define Maxm 1000005 #define lowbit(x) x&(-x) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define PI acos(-1.0) #define make_pair MP #define LL long long #define Inf (1LL<<62) #define inf 0x3f3f3f3f #define re freopen("in.txt","r",stdin) #define wr freopen("out.txt","w",stdout) using namespace std; struct BigNum { int num[55]; int size; int tsize; BigNum() { memset(num,0,sizeof(num)); size=1; tsize=1; } friend BigNum operator +(BigNum a,BigNum b) { BigNum c; int i; for(i=0;i
                 
                  b.tsize&&a.tsize>50) { c.num[i]+=a.num[i]+b.num[i+1]; c.num[i+1]+=c.num[i]/10; c.num[i]%=10; } else if(a.tsize
                  
                   50) { c.num[i]+=a.num[i+1]+b.num[i]; c.num[i+1]+=c.num[i]/10; c.num[i]%=10; } else { c.num[i]+=a.num[i]+b.num[i]; c.num[i+1]+=c.num[i]/10; c.num[i]%=10; } } c.size=c.num[i]?i+1:i; int tmp=c.size-1; if(max(a.tsize,b.tsize)<=50) c.tsize=max(a.size,b.size); else c.tsize=max(a.tsize,b.tsize); if(c.size>50) { int len=50; BigNum t; while(len--) t.num[len]=c.num[tmp--]; t.tsize=max(a.tsize,b.tsize)+1; c=t; c.size=50; } return c; } void output(BigNum n) { int i; for(i=n.size-1;i>=0;i--) printf("%d",n.num[i]); cout<<" "<
                   
                    =0&&cnt<=40;i--,cnt++) { if(p->next[s[i]]==NULL) { temp=new TrieNode; p->next[s[i]]=temp; } p=p->next[s[i]]; p->index=min(p->index,index); } } int search(int *s,int len) { int i; TrieNode *p=root; TrieNode *tmp; for(i=0;i
                    
                     next[s[i]]==NULL) return -1; p=p->next[s[i]]; } return p->index; } void Delete(TrieNode *node) { int i; for(i=0;i<10;i++) { if(node->next[i]) Delete(node->next[i]); delete node->next[i]; node->next[i]=0; } } int main() { int n,ncase=1,arr[50]; char str[50]; fib[0].num[0]=1;fib[1].num[0]=1; root=new TrieNo
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj_2752 Seek the Name, Seek th.. 下一篇hdu 1009 233 Matrix 矩阵构造 --..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·MySQL 安装及连接-腾 (2025-12-25 06:20:28)
·MySQL的下载、安装、 (2025-12-25 06:20:26)
·MySQL 中文网:探索 (2025-12-25 06:20:23)
·Shell脚本:Linux Sh (2025-12-25 05:50:11)
·VMware虚拟机安装Lin (2025-12-25 05:50:08)