BZOJ1002(FJOI2007)[轮状病毒]--找规律+高精度

【链接】
bzoj1002

【解题报告】

慢慢找(kan)规(ti)律(jie)就可以得出这个公式

fn=3fn1fn2+2

其中 f1=1     f2=5

#include
#include
#include
using namespace std;
const int maxn=105,maxv=55;
int n;
struct Bignum
{
    int s[maxv];
    Bignum operator + (const Bignum &a) const{
        Bignum c;
        memset(c.s,0,sizeof(c));
        c.s[0]=max(s[0],a.s[0]);
        for (int i=1; i<=c.s[0]; i++) c.s[i]+=s[i]+a.s[i],c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
        while (c.s[c.s[0]+1]) c.s[0]++;
        return c;
    }
    Bignum operator - (const Bignum &a) const{
        Bignum c;
        memset(c.s,0,sizeof(c));
        c.s[0]=s[0];
        for (int i=1; i<=c.s[0]; i++)
        {
            c.s[i]+=s[i]-a.s[i];
            if (c.s[i]<0) c.s[i]+=10,c.s[i+1]--;
        }
        while (c.s[0]>1&&!c.s[c.s[0]]) c.s[0]--;
        return c;
    }
    Bignum operator * (const Bignum &a) const{
        Bignum c;
        memset(c.s,0,sizeof(c));
        c.s[0]=s[0]+a.s[0];
        for (int i=1; i<=s[0]; i++)
         for (int j=1; j<=a.s[0]; j++) c.s[i+j-1]+=s[i]*a.s[j];
        for (int i=1; i<=c.s[0]; i++) c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
        while (c.s[c.s[0]+1]) c.s[0]++;
        while (c.s[0]>1&&!c.s[c.s[0]]) c.s[0]--;
        return c;
    }
}f[maxn],a2,a3;
Bignum Num(int x)
{
    Bignum c; memset(c.s,0,sizeof(c));
    while (x) c.s[++c.s[0]]=x%10,x/=10;
    return c;
}
int main()
{
    freopen("1002.in","r",stdin);
    freopen("1002.out","w",stdout);
    scanf("%d",&n); f[1]=Num(1); f[2]=Num(5); a3=Num(3); a2=Num(2);
    for (int i=3; i<=n; i++) f[i]=a3*f[i-1]-f[i-2]+a2;
    for (int i=f[n].s[0]; i; i--) printf("%d",f[n].s[i]); putchar(10);
    return 0;
}

你可能感兴趣的:(BZOJ题解,找规律,高精度,BZOJ题解,By,Greninja,部分常见刷题网站的题解,By,Greninja)