題意
T組詢問,每次給定n,輸出An。
T<=20, n<=10^100 題解
這道題容易想太多,其實不用對遞推式進行什麼處理,只需要用特別的方法記憶化即可。
注意到雖然n的範圍大,但真正有用的很少。
例如當n=31時:
有用的節點只有O(logn)個,如果我們能實現記憶化,可以實現logn的複雜度,再搞個高精即可。
但是數字這麼大,如何記憶化呢。實際上從上圖我們已經可以發現,只需要一對一對數一起推就可以了。
奇偶討論往下推
2i+1, 2i ————– i, i+1
2i, 2i-1 ————– i, i-1
這一對數只和下一對數有關,這樣推層數就一直是1了。總複雜度O(T*logn*高精)
好像還可以寫個 STL 平衡樹把算過的節點放進去,然後二分實現記憶化,複雜度多一個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;}