This problem is actually water problem come, beg Fibonacci number of the first 4 and the last 4, in n==40 here demarcation open. After 4 bit is not difficult to seek, because N reached the scale of the 10^18, so can only use the matrix fast power to seek, but in the output after 4 bits must pay attention to the leading 0 of the processing (I was here wa a pitch, but also looked at other people's code to find).
The first 4 words are a little bit difficult to deal with, and I started out thinking about what to do with log10 (f (n))? Put f[n]= F[n-1]+f[n-2]? No, log on + operation can not expand, I looked for a long time also did not find what can let F[n] expand into the form of multiplication or power, the Internet searched the next key, only to find that others unexpectedly is to use the formula to deal with (I thought, but because of the "-" operation so it is directly ignored, But I did not expect to be able to actually slightly work out the "-" to remove the "-": f (n) = 1/sqrt (5) (((1+SQRT (5)/2) ^n-((1-SQRT (5)/2) ^n);
When specifically implemented LOG10 F[n] is approximately equal to ((1+SQRT (5))/2) ^n/sqrt (5), here we put ((1-SQRT (5))/2) ^n This is ignored, because when the n>=40, this number is already small can be ignored. So log10 F[n] can be reduced to N*LOG10 ((1+SQRT (5))/2)-log10 sqrt (5), not to mention, attached code:
1#include <cstdio>2#include <cmath>3#include <cstring>4 #definefor (i,s,t) for (int i=s; i<=t; ++i)5typedefLong LongLL;6 ConstLL mod=10000;7 8 structmatrix{9 LL a,b,c,d;TenMatrix (LL a=0, LL b=0, LL c=0, LL d=0): A (a), B (b), C (c), D (d) {} OneMatrixoperator*(ConstMatrix &m2)Const { A returnMatrix ((A*m2.a%mod+b*m2.c%mod)%mod, (A*m2.b%mod+b*m2.d%mod)%mod, (C*m2.a%mod+d*m2.c%mod)%mod, (c*m2.b%mod+d*m2.d% MoD)%MoD); - } -A1,1,1,0), E (1,0,0,1); the - Matrix Quick_mod (Matrix M, LL b) { - Matrix Res (E); - while(b) { + if(b&1) res= res*m; -M= m*m; +b>>=1; A } at returnRes; - } - - voidSolve (LL N) { -LL last= Quick_mod (a,n-1). A; - //int last= int (quick_mod (a,n-1). A); in DoubleM= N*log10 ((1+SQRT (5.0))/2)-log10 (sqrt (5.0)); -M-=LL (m); toll first= ll (Pow (10.0, m) * +); + //there must be 04d!. The rear 4-bit leading 0 also outputs! -printf"%i64d...%04i64d\n", first,last); the } * $LL f[ +]= {0,1,1};Panax Notoginseng voidInitintn= the){ -for (I,3, N) f[i]= f[i-1]+f[i-2]; the } + A intMain () { the init (); + LL N; - while(~SCANF ("%i64d",&N)) { $ if(n< +) printf ("%i64d\n", F[n]); $ Elsesolve (n); - } - return 0; the}
It is amazing, with i64d to output the top 4 is 0ms (direct%04d words is 15ms), as if the first row on the first page of the HDU, haha ~ ~ attached a picture:
HDU 3117 Fibonacci Numbers