BZOJ 1002 - 轮状病毒 - [基尔霍夫矩阵(待补)+高精度][FJOI2007]

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1002

Description
  轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的。一个N轮状基由圆环上N个不同的基原子
和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道。如下图所示


  N轮状病毒的产生规律是在一个N轮状基中删去若干条边,使得各原子之间有唯一的信息通道,例如共有16个不
同的3轮状病毒,如下图所示


  现给定n(N<=100),编程计算有多少个不同的n轮状病毒
Input
  第一行有1个正整数n

Output
  计算出的不同的n轮状病毒数输出

Sample Input
3
Sample Output
16

题意:

递推公式 $f[i] = f[i-1] \times 3 - f[i-2] + 2$。

题解:

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
 
struct BigInt
{
    static const int maxdigit=100;
    int len,d[maxn];
 
    void clean(){while(len>1 && !d[len-1]) len--;}
    string str()const
    {
        string s;
        for(int i=0;i<len;i++) s+=d[len-1-i]+'0';
        return s;
    }
 
    BigInt(){memset(d,0,sizeof(d));len=1;}
    BigInt(int num){*this=num;}
    BigInt(char* num){*this=num;}
 
    bool operator<(const BigInt& oth)const
    {
        if(len!=oth.len) return len<oth.len;
        for(int i=len-1;i>=0;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
        return false;
    }
    bool operator>(const BigInt& oth)const{return oth<*this;}
    bool operator<=(const BigInt& oth)const{return !(oth<*this);}
    bool operator>=(const BigInt& oth)const{return !(*this<oth);}
    bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
    bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);}
 
    BigInt operator=(const char* num)
    {
        memset(d,0,sizeof(d));
        len=strlen(num);
        for(int i=0;i<len;i++) d[i]=num[len-1-i]-'0';
        clean();
        return *this;
    }
    BigInt operator=(int num)
    {
        char s[20];
        sprintf(s,"%d",num);
        return *this=s;
    }
    BigInt operator+(const BigInt& oth)const
    {
        BigInt c;
        c.len=max(len,oth.len);
        for(int i=0;i<=c.len;i++) c.d[i]=0;
        for(int i=0;i<c.len;i++)
        {
            c.d[i]+=(i<len?d[i]:0)+(i<oth.len?oth.d[i]:0);
            c.d[i+1]+=c.d[i]/10;
            c.d[i]%=10;
        }
        c.len+=(c.d[c.len]>0);
        c.clean();
        return c;
    }
    BigInt operator-(const BigInt& oth)const
    {
        BigInt c=*this;
        if(c<oth) printf("Produce negative number!\n");
        int i;
        for(i=0;i<oth.len;i++)
        {
            c.d[i]-=oth.d[i];
            if(c.d[i]<0) c.d[i]+=10, c.d[i+1]--;
        }
        while(c.d[i]<0) c.d[i++]+=10, c.d[i]--;
        c.clean();
        return c;
    }
    BigInt operator*(const BigInt& oth)const
    {
        BigInt c;
        for(int i=0;i<len;i++) for(int j=0;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
        for(int i=0;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+1]+=c.d[i]/10, c.d[i]%=10;
        c.clean();
        return c;
    }
    BigInt operator/(const BigInt& oth)const
    {
        BigInt c=*this, r=0;
        for(int i=0;i<len;i++)
        {
            r=r*10+c.d[len-1-i];
            int j;
            for(j=0;j<10;j++) if(r<oth*(j+1)) break;
            c.d[len-1-i]=j;
            r=r-oth*j;
        }
        c.clean();
        return c;
    }
    BigInt operator%(const BigInt& oth)
    {
        BigInt r=0;
        for(int i=0;i<len;i++)
        {
            r=r*10+d[len-1-i];
            int j;
            for(j=0;j<10;j++) if(r<oth*(j+1)) break;
            r=r-oth*j;
        }
        return r;
    }
    BigInt operator+=(const BigInt& oth)
    {
        *this=*this+oth;
        return *this;
    }
    BigInt operator*=(const BigInt& oth)
    {
        *this=*this*oth;
        return *this;
    }
    BigInt operator-=(const BigInt& oth)
    {
        *this=*this-oth;
        return *this;
    }
    BigInt operator/=(const BigInt& oth)
    {
        *this=*this/oth;
        return *this;
    }
};
istream& operator>>(istream& in, BigInt& x)
{
    string s;
    in>>s;
    x=s.c_str();
    return in;
}
ostream& operator<<(ostream& out,const BigInt& x)
{
    out<<x.str();
    return out;
}
 
int n;
BigInt f[maxn];
int main()
{
    cin>>n;
    f[1]=1;
    f[2]=5;
    for(int i=3;i<=n;i++) f[i]=(f[i-1]*3-f[i-2]+2);
    cout<<f[n]<<endl;
}

猜你喜欢

转载自www.cnblogs.com/dilthey/p/9824576.html