Revenge of Fibonacci
Test instructions: The first n bits (n<=40) of the first 100000 items of a given Fibonacci sequence; ask you this is the prefix of the Fibonacci series? If not within the first 100000 items, output-1;
Idea: Directly using the array to simulate the addition, and then use the Trie tree to insert the search, but the general use of New Trie () code is MLE. Instead I wrote before, directly get the array size of the Maxnode version of memory acceptable, and there is one point is the accuracy of the first 40 bits, because it is calculated by the Finboncci series, not the system, so 1 of the carry will not form a recursive form, but also can not only calculate to 40 + Bit: The code is calculated to the first 60 bits, and then only to the trie 40 bits, and one thing is to maintain the value of the first 60 bits, that is, when the number of bits increased, gradually to the status of the house;
#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 Ll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}Const intMAXL = 1e5* $+7;Const intSigma_size =Ten;structtrie{intVal[maxl],sz; intch[maxl][Ten]; Trie () {sz=1; MS0 (ch[0]);} voidInsert (Char*s,intv) {intU =0, Len =strlen (s); Rep0 (i,0, Len) { intc = s[i]-'0'; if(Ch[u][c] = =0) {MS0 (Ch[sz]); VAL[SZ]=v; CH[U][C]= sz++; } u=Ch[u][c]; } if(Val[u] = =0) Val[u] = v;//cannot be directly covered; } intFind (Char*s) {intU =0, Len =strlen (s); Rep0 (i,0, Len) { intc = s[i]-'0'; if(Ch[u][c] = =0)return 0; U=Ch[u][c]; } returnVal[u]; }}trie;inta[ -],b[ -],c[ -];voidinit () {Chars[ -] = {'1'}; a[0] =1; b[0] =1; Trie. Insert (S,1); Rep0 (i,2,100000){ intR =0, cnt =0; Rep1 (J,0, -) {C[j]= a[j]+b[j]+R; R= c[j]/Ten; C[J]= c[j]%Ten; } intID =-1; Rep_1 (J, -,0){ if(ID <0&& c[j]) id =J; if(~ID) {s[cnt+ +] = c[j]+'0'; } if(CNT >= +) Break; } s[cnt]=' /'; Trie. Insert (S,i+1); if(ID >= -){//prevent carry error, give up the back one;Rep1 (J,1, -) C[j-1] = c[j],b[j-1] =B[j]; c[ -] = b[ -] =0; } rep1 (J,0, -) {A[j]=B[j]; B[J]=C[j]; } }}intMain () {init (); intKase =1, T; Read1 (T); Charstr[ $]; while(t--) {scanf ("%s", str); printf ("Case #%d:%d\n", Kase++,trie. Find (str)-1); } return 0;}
Hdu 4099 Revenge of Fibonacci trie tree with analogue digit addition