[BZOJ1002] [FJOI2007] 轮状病毒

详细的做法和证明过程网上已经有了,这里就不加以赘述。其实打个表就能看出来关系咯。

ps:我这里用&来代替一次计算的

#include "stdio.h"
#include "memory.h"

using namespace std;

struct HugeInt {
       int ans[61];
       int l;
       void output()
       {    int i;
            for (i=l; i>=1; i--){
                putchar(ans[i]+'0');
            }
            puts("");
       }
       void copy(HugeInt copy)
       {l=copy.l; for (int i=l; i>=1; i--) ans[i]=copy.ans[i];}
       void Clean(){ l=0; memset(ans,0,sizeof(ans));}
} f[105];
// f[i]=3*f[i-1]-f[i-2]+2

HugeInt operator & (HugeInt a,HugeInt b){
        HugeInt Return;
        Return. Clean();
        int i;
        Return.l=a.l;
        for (i=1; i<=Return.l; i++){
            Return.ans[i]=a.ans[i]*3-b.ans[i];
        }
        Return.ans[1]+=2;
        for (i=1; i<=Return.l; i++){
            if (Return.ans[i]>=10){
               Return.ans[i+1]+=Return.ans[i]/10;
               Return.ans[i]%=10;
            }
            if (Return.ans[i]<0){
               Return.ans[i+1]--;
               Return.ans[i]+=10;
            }
        }
        if( Return.ans[Return.l+1]) ++Return.l;
        return Return;
}

int main ()
{   freopen ("1002.in" ,"r" ,stdin);
    freopen ("1002.out" ,"w" ,stdout);
    f[1].l=f[1].ans[1]=f[2].l=1;
    f[2].ans[1]=5;
    int n;
    scanf("%d",&n);
    for (int i=3;i<=n;i++){
        f[i].copy(f[i-1]&f[i-2]);
    }
    f[n].output();
    return 0;
}


你可能感兴趣的:([BZOJ1002] [FJOI2007] 轮状病毒)