标签:des style http color io os java ar strong
15 1 12 123 1234 12345 9 98 987 9876 98765 89 32 51075176167176176176 347746739 5610
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
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <set> #include <map> #include <list> #include <queue> #include <stack> #include <deque> #include <vector> #include <bitset> #include <cmath> #include <utility> #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<max(a.size,b.size);i++) { if(a.tsize>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<b.tsize&&b.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<<" "<<n.tsize; printf("\n"); } }; BigNum fib[3]; struct TrieNode { int index; TrieNode *next[10]; TrieNode() { index=inf; memset(next,0,sizeof(next)); } }; TrieNode *root=NULL; void CreatTree(int *s,int len,int index) { int i,cnt; TrieNode *p=root; TrieNode *temp; for(i=len-1,cnt=0;i>=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<len;i++) { if(p->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 TrieNode; CreatTree(fib[0].num,fib[0].size,0); CreatTree(fib[1].num,fib[1].size,1); //re;wr; for(int i=2;i<100000;i++) { fib[i%3]=fib[(i-1)%3]+fib[(i-2)%3]; //fib[i%3].output(fib[i%3]); CreatTree(fib[i%3].num,fib[i%3].size,i); } scanf("%d",&n); while(n--) { printf("Case #%d: ",ncase++); memset(arr,0,sizeof(arr)); scanf("%s",str); int len=strlen(str); for(int i=0;i<len;i++) arr[i]=str[i]-'0'; printf("%d\n",search(arr,len)); memset(str,'\0',sizeof(str)); } Delete(root); return 0; }
标签:des style http color io os java ar strong
原文地址:http://blog.csdn.net/hqu_fritz/article/details/39274627