the
T Group query, each given n, output an.
T<=20, n<=10^100
This problem is easy to think too much, in fact, do not have to do with the recurrence of what to do, only to use a special method of memory can be.
Note that although the range of n is large, there is very little that really works.
For example, when n=31:
The useful node is only O (logn), if we can realize the memory, we can realize the complexity of LOGN, and then get a high precision.
But the number is so large, how to remember it. As a matter of fact, we can see from the above figure that we just need a one-to-one logarithm to push along.
Odd and even discussion push down
2i+1, 2i ———— – I, i+1
2i, 2i-1 ———— – I, i-1
This pair of numbers is only related to the next logarithm, so that the number of push layers is always 1. Total complexity O (t*logn* high precision)
It seems that you can also write an STL balance tree to put the calculated nodes in, and then realize the memory of the two, more complex one logn.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int con=100000000;
typedef long Long LL;
struct int{LL a[505];
Int (LL x=0) {memset (a,0,sizeof (a)); do A[++a[0]]=x%con, X/=con; while (x);}
void Read () {memset (a,0,sizeof (a)); Char s[105]; scanf ("%s", s+1);
int Len=strlen (s+1);
for (int i=len;i>0;i-=8) {a[0]++; for (int j=max (i-7,1); j<=i;j++) a[a[0]]=a[a[0]]*10+s[j]-' 0 ';}
} void Write () {printf ("%lld", A[a[0]]);
for (int i=a[0]-1;i>=1;i--) printf ("%08lld", A[i]);
int operator + (const int &b) {Int C; C.a[0]=max (A[0],b.a[0]);
for (int i=1;i<=c.a[0];i++) c.a[i]+=a[i]+b.a[i], C.a[i+1]+=c.a[i]/con, C.a[i]%=con;
if (c.a[c.a[0]+1]) c.a[0]++;
return C;
int operator/(const int &x) {Int C; c.a[0]=a[0]; LL now=0;
for (int i=a[0];i;i--) now=now*con+a[i], c.a[i]=now/x, now%=x; C.A[0]=A[0]; if (C.A[0]>1&&!c.a[c.a[0]]) c.a[0]--;
return C;
}
}; void Get (Int k1,int k2,int &res1,int &res2) {if (k1.a[0]==1&&k1.a[1]==0&&k2.a[0]==1&&
K2.a[1]==1) {res1=int (0); Res2=int (1);
Return
} Get (K1/2, (K2+int (1))/2,res1,res2);
if (k1.a[1]&1) res1=res1+res2;
else Res2=res1+res2;
int _test;
int main () {freopen ("bzoj2656.in", "R", stdin);
Freopen ("Bzoj2656.out", "w", stdout);
scanf ("%d", &_test);
while (_test--) {Int x; x.read (); Int ans,t;
Get (X,x+int (1), ans,t); Ans.write ();
printf ("\ n");
return 0; }